fix(cache): complete atomic shard writes on Windows
Merged
NA
4mo ago+26+26+01234567890123456789-1-1-0123456789
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 behavior | Current upstream location | Adaptation |
|---|---|---|
| Flush and sync the monolithic temporary cache, then drop its writer before replacement | write_shard_with_limit temporary envelope writer | Apply the same handle ordering to each shard writer |
Reopen the installed cache read/write before sync_all() | Final shard durability sync | Use OpenOptions::new().read(true).write(true) |
| Monolithic cache layout and schema | Sharded envelopes and per-parser versions | Deliberately excluded; no format or identity change |
| Atomic replacement implementation | Shared fs_atomic::replace_file with #906 retry behavior | Deliberately 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=1cargo test -p tokscale-core test_save_if_dirty_marks_cache_clean -- --test-threads=1cargo fmt --all -- --checkcargo clippy --locked --workspace --all-features -- -D warningscargo test --workspace --all-featurescargo build --release -p tokscale-cligit 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 WindowsFlushFileBuffers. - 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.
- Drop the temporary shard writer before atomic replace to avoid carrying an open source handle into