fix(cache): complete atomic shard writes on Windows

Merged
NA
4mo ago
+26-1

Fixes #913

Summary

  • Release the temporary cache shard writer before atomic replacement so the cache does not keep its own source handle open across MoveFileExW.
  • Reopen the installed shard with read and write access before the final durability sync, matching the access requirements of Windows FlushFileBuffers.
  • Add a direct shard write/read regression without changing cache format, parser versions, shard identity, retry policy, or TUI warning behavior.

Current-upstream adaptation

Validated downstream behaviorCurrent upstream locationAdaptation
Flush and sync the monolithic temporary cache, then drop its writer before replacementwrite_shard_with_limit temporary envelope writerApply the same handle ordering to each shard writer
Reopen the installed cache read/write before sync_all()Final shard durability syncUse OpenOptions::new().read(true).write(true)
Monolithic cache layout and schemaSharded envelopes and per-parser versionsDeliberately excluded; no format or identity change
Atomic replacement implementationShared fs_atomic::replace_file with #906 retry behaviorDeliberately unchanged

The source behavior was recovered into TokenBar PR #59. The Windows validation ancestry and full downstream evidence are recorded in the handoff comment.

Verification

  • cargo test -p tokscale-core test_write_shard_round_trips_after_atomic_replace -- --test-threads=1
  • cargo test -p tokscale-core test_save_if_dirty_marks_cache_clean -- --test-threads=1
  • cargo fmt --all -- --check
  • cargo clippy --locked --workspace --all-features -- -D warnings
  • cargo test --workspace --all-features
  • cargo build --release -p tokscale-cli
  • git diff --check

Windows evidence boundary

The downstream handle-ordering fix was executed and validated on Windows. This PR adapts that invariant to the current shard writer. Upstream Build Native checks Windows x64 and arm64 compilation only; it does not execute this regression on Windows. Unless an additional Windows runner runs the test, this PR does not claim independent upstream Windows runtime verification.


Summary by cubic

Fixes atomic shard writes on Windows by closing the temp writer before replacement and reopening the final shard for a proper flush. Prevents handle conflicts and ensures cache durability after writes.

  • Bug Fixes
    • Drop the temporary shard writer before atomic replace to avoid carrying an open source handle into MoveFileExW.
    • Reopen the installed shard with read+write before the final sync_all() to satisfy Windows FlushFileBuffers.
    • Add a regression test to verify shard write/read round-trip after replace.
    • No changes to cache format, shard identity, parser versions, retry policy, or TUI behavior.