Pipeline: Cache::get_or_convert fails on Windows concurrent publication race #890

Open
opened 2026-11-26 04:14:07 +00:00 by github-bot · 5 comments
Owner

@⁠davi713albano-coder on GitHub · 2026-11-24 13:33 UTC · original

What's wrong

In crates/victory-pipeline/src/cache.rs, Cache::get_or_convert writes to a staging file .tmp-<pid>-<n> and publishes it via std::fs::rename(&tmp, &path). When rename fails, it removes &tmp and unconditionally returns the error.

On Windows, if two worker threads (or two processes) convert the same content-addressed key concurrently and the winner's published file is opened for reading before the loser's std::fs::rename(&tmp, &path) runs, Windows rejects MoveFileExW with a sharing/access violation even though path is now a valid published file for that exact immutable BLAKE3 key.

Fix

Separate file.write_all error handling from std::fs::rename error handling:

  • If write_all fails, remove &tmp and return the error.
  • If rename fails, remove &tmp; if path.is_file() is now true (another worker won the race to publish this content-addressed entry), return Ok(path). When path is not a file (e.g., an obstructing directory as in failed_publication_removes_the_staging_file), the error is still propagated.

Because external PR creation is restricted (collaborators_only), the CI-green branch is ready on my fork:

Tests & Verification

Added 2 unit tests in victory-pipeline:

  • concurrent_publication_reuses_existing_entry_and_removes_staging (simulates a concurrent writer publishing target and holding it open on Windows with FILE_SHARE_READ only so rename deterministically fails; verifies Ok(target) is returned and the staging file is cleaned up)
  • concurrent_threads_converting_same_key_all_succeed (8 barrier-synchronized threads calling get_or_convert on the same key and reading the output immediately; verifies all succeed and no .tmp-* files remain)

Verified on GitHub Actions (windows-latest) using upstream ci.yml (cargo clippy --workspace --all-targets --locked -- -D warnings, --all-features, and cargo test --workspace --locked including xtask asset-policy check):

No game data used. AI-assisted.

**@⁠davi713albano-coder** on GitHub · 2026-11-24 13:33 UTC · [original](https://github.com/Marvisak/VIctory/issues/890) ### What's wrong In `crates/victory-pipeline/src/cache.rs`, `Cache::get_or_convert` writes to a staging file `.tmp-<pid>-<n>` and publishes it via `std::fs::rename(&tmp, &path)`. When `rename` fails, it removes `&tmp` and unconditionally returns the error. On Windows, if two worker threads (or two processes) convert the same content-addressed key concurrently and the winner's published file is opened for reading before the loser's `std::fs::rename(&tmp, &path)` runs, Windows rejects `MoveFileExW` with a sharing/access violation even though `path` is now a valid published file for that exact immutable BLAKE3 key. ### Fix Separate `file.write_all` error handling from `std::fs::rename` error handling: - If `write_all` fails, remove `&tmp` and return the error. - If `rename` fails, remove `&tmp`; if `path.is_file()` is now true (another worker won the race to publish this content-addressed entry), return `Ok(path)`. When `path` is not a file (e.g., an obstructing directory as in `failed_publication_removes_the_staging_file`), the error is still propagated. Because external PR creation is restricted (`collaborators_only`), the CI-green branch is ready on my fork: - **Branch:** https://github.com/davi713albano-coder/VIctory/commits/pipeline/concurrent-cache-publish - **Commit:** `32e7e81` (based on `main` `ffe310e`, `crates/victory-pipeline/src/cache.rs` +111/-3) - **Compare:** https://github.com/Marvisak/VIctory/compare/main...davi713albano-coder:pipeline/concurrent-cache-publish ### Tests & Verification Added 2 unit tests in `victory-pipeline`: - `concurrent_publication_reuses_existing_entry_and_removes_staging` (simulates a concurrent writer publishing `target` and holding it open on Windows with `FILE_SHARE_READ` only so `rename` deterministically fails; verifies `Ok(target)` is returned and the staging file is cleaned up) - `concurrent_threads_converting_same_key_all_succeed` (8 barrier-synchronized threads calling `get_or_convert` on the same key and reading the output immediately; verifies all succeed and no `.tmp-*` files remain) Verified on GitHub Actions (`windows-latest`) using upstream `ci.yml` (`cargo clippy --workspace --all-targets --locked -- -D warnings`, `--all-features`, and `cargo test --workspace --locked` including `xtask` asset-policy check): - `victory-pipeline`: 6 passed, 0 failed (full workspace: 344 passed, 0 failed, 6 ignored) - CI run: https://github.com/davi713albano-coder/VIctory/actions/runs/45403473507 No game data used. AI-assisted.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:07 +00:00
Author
Owner

@⁠DaniiL35 on GitHub · 2026-11-24 13:42 UTC · original

Verified branch 32e7e81 (on top of ffe310e):

Concurrency & Correctness Analysis

  • Root Cause Verified: On Windows, std::fs::rename delegates to MoveFileExW(..., MOVEFILE_REPLACE_EXISTING). When multiple worker threads convert the same key concurrently, the winner successfully renames its .tmp file to path. If a consumer or another thread opens path (default Windows sharing without FILE_SHARE_DELETE), concurrent losers attempting to rename their own staging files over path receive ERROR_SHARING_VIOLATION / ERROR_ACCESS_DENIED. The unpatched code treated this as a hard error.
  • Content-Address Invariant: Because cache paths are strictly content-addressed by BLAKE3 hash (converter.id(), converter.version(), input), an entry at path is guaranteed to be bit-for-bit identical to what the losing worker produced.
  • Atomicity Invariant: Because entries are only ever placed at path via atomic rename from completed staging files, an existing file at path is guaranteed to be fully written and not a partial write.
  • Error Propagation Preserved: If rename fails due to a directory collision (as in failed_publication_removes_the_staging_file), path.is_file() is false and the error is correctly returned.
  • Resource Hygiene: In both error paths (written failure and rename failure), the temporary staging file is guaranteed to be removed, preventing orphan .tmp-* files from accumulating in the cache hierarchy.

Test Coverage

  • concurrent_publication_reuses_existing_entry_and_removes_staging: Deterministically exercises the Windows sharing violation by holding target open with share_mode(1) (FILE_SHARE_READ without FILE_SHARE_DELETE). Verified that the winner's file is preserved, the loser's staging file is cleaned up, and Ok(target) is returned.
  • concurrent_threads_converting_same_key_all_succeed: 8 barrier-synchronized threads racing on the same key all succeed, and read_dir verifies exactly 1 file remains in the bucket directory.
  • #[cfg(windows)] gating on OpenOptionsExt correctly keeps the test suite portable and warning-free across non-Windows targets.

Policy & Toolchain

  • Complies with Rule #1 (Asset Policy): Pure synthetic mock converters (Upper, Racing) with synthetic text byte slices.
  • cargo xtask policy: clean.
  • Clean merge on top of main (ffe310e).

Reviewed and verified. AI-assisted with Antigravity.

**@⁠DaniiL35** on GitHub · 2026-11-24 13:42 UTC · [original](https://github.com/Marvisak/VIctory/issues/890#issuecomment-7133227449) Verified branch `32e7e81` (on top of `ffe310e`): ### Concurrency & Correctness Analysis - **Root Cause Verified**: On Windows, `std::fs::rename` delegates to `MoveFileExW(..., MOVEFILE_REPLACE_EXISTING)`. When multiple worker threads convert the same key concurrently, the winner successfully renames its `.tmp` file to `path`. If a consumer or another thread opens `path` (default Windows sharing without `FILE_SHARE_DELETE`), concurrent losers attempting to rename their own staging files over `path` receive `ERROR_SHARING_VIOLATION` / `ERROR_ACCESS_DENIED`. The unpatched code treated this as a hard error. - **Content-Address Invariant**: Because cache paths are strictly content-addressed by BLAKE3 hash (`converter.id()`, `converter.version()`, `input`), an entry at `path` is guaranteed to be bit-for-bit identical to what the losing worker produced. - **Atomicity Invariant**: Because entries are only ever placed at `path` via atomic rename from completed staging files, an existing file at `path` is guaranteed to be fully written and not a partial write. - **Error Propagation Preserved**: If `rename` fails due to a directory collision (as in `failed_publication_removes_the_staging_file`), `path.is_file()` is `false` and the error is correctly returned. - **Resource Hygiene**: In both error paths (`written` failure and `rename` failure), the temporary staging file is guaranteed to be removed, preventing orphan `.tmp-*` files from accumulating in the cache hierarchy. ### Test Coverage - `concurrent_publication_reuses_existing_entry_and_removes_staging`: Deterministically exercises the Windows sharing violation by holding `target` open with `share_mode(1)` (`FILE_SHARE_READ` without `FILE_SHARE_DELETE`). Verified that the winner's file is preserved, the loser's staging file is cleaned up, and `Ok(target)` is returned. - `concurrent_threads_converting_same_key_all_succeed`: 8 barrier-synchronized threads racing on the same key all succeed, and `read_dir` verifies exactly 1 file remains in the bucket directory. - `#[cfg(windows)]` gating on `OpenOptionsExt` correctly keeps the test suite portable and warning-free across non-Windows targets. ### Policy & Toolchain - Complies with Rule #1 (Asset Policy): Pure synthetic mock converters (`Upper`, `Racing`) with synthetic text byte slices. - `cargo xtask policy`: clean. - Clean merge on top of `main` (`ffe310e`). Reviewed and verified. AI-assisted with Antigravity.
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-24 13:45 UTC · original

I checked the same-key race with two independent processes on native Windows 10. The first process paused inside its converter. The second published the entry and held it open with FILE_SHARE_READ while the first tried to publish.

At ffe310e, the first process received the Windows access/sharing error in all 10 rounds. With the candidate, both processes succeeded in all 10 rounds. Every round ended with one readable cache entry containing the expected bytes and zero staging files. All six victory-pipeline library tests passed on the candidate. Tested commit 32e7e81

AI-assisted. Verified with synthetic bytes in temporary caches.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-24 13:45 UTC · [original](https://github.com/Marvisak/VIctory/issues/890#issuecomment-7133296396) I checked the same-key race with two independent processes on native Windows 10. The first process paused inside its converter. The second published the entry and held it open with `FILE_SHARE_READ` while the first tried to publish. At `ffe310e`, the first process received the Windows access/sharing error in all 10 rounds. With the candidate, both processes succeeded in all 10 rounds. Every round ended with one readable cache entry containing the expected bytes and zero staging files. All six `victory-pipeline` library tests passed on the candidate. [Tested commit `32e7e81`](https://github.com/davi713albano-coder/VIctory/commit/32e7e8121eb2d1948d0fee899ced679c17c96169) AI-assisted. Verified with synthetic bytes in temporary caches.
Author
Owner

@⁠LAKSHYA-cmd-tech on GitHub · 2026-11-24 13:51 UTC · original

Verified and merged downstream in PR #26. Passing unit tests concurrent_publication_reuses_existing_entry_and_removes_staging and concurrent_threads_converting_same_key_all_succeed pass cleanly under Windows 11.

**@⁠LAKSHYA-cmd-tech** on GitHub · 2026-11-24 13:51 UTC · [original](https://github.com/Marvisak/VIctory/issues/890#issuecomment-7133450581) Verified and merged downstream in PR #26. Passing unit tests concurrent_publication_reuses_existing_entry_and_removes_staging and concurrent_threads_converting_same_key_all_succeed pass cleanly under Windows 11.
Author
Owner

@⁠oddmaniv on GitHub · 2026-11-24 20:11 UTC · original

Independent integration coverage for this cache race is available in https://github.com/oddmaniv/VIctory/pull/2 (commit b9ae66521be52a7cfebbdfd3eff3272b684a5419), combining the existing fixes from #1109 (DocBlack89, published by divyeshganta) and #1324 (mgunes1311-lab).

The new synthetic Windows regression forces both a stale staging-name collision and a published destination held open without delete sharing in the same get_or_convert call. It runs in an isolated test process, with no production reset hook. With the same test, main fails with Windows error 80, #1109 alone fails with error 5, and #1324 alone fails with error 80; the combined implementation passes. It also checks the published bytes, preservation of the stale file, and cleanup of the current writer's staging file.

On Windows with Rust 1.98.1, the pipeline suite passes all 9 tests; both workspace Clippy variants, changed-file formatting and asset policy pass. The unfiltered workspace run is 346 passed, 1 failed, 6 ignored; the same display-deferrals assertion also fails on the base revision (341/1/6).

This is integration/regression evidence, not a replacement for the existing PRs or a claim of gameplay improvement. The new test can be adapted into whichever implementation lands. Synthetic inputs only; AI-assisted.

**@⁠oddmaniv** on GitHub · 2026-11-24 20:11 UTC · [original](https://github.com/Marvisak/VIctory/issues/890#issuecomment-7143168321) Independent integration coverage for this cache race is available in https://github.com/oddmaniv/VIctory/pull/2 (commit `b9ae66521be52a7cfebbdfd3eff3272b684a5419`), combining the existing fixes from #1109 (DocBlack89, published by divyeshganta) and #1324 (mgunes1311-lab). The new synthetic Windows regression forces both a stale staging-name collision and a published destination held open without delete sharing in the same `get_or_convert` call. It runs in an isolated test process, with no production reset hook. With the same test, main fails with Windows error 80, #1109 alone fails with error 5, and #1324 alone fails with error 80; the combined implementation passes. It also checks the published bytes, preservation of the stale file, and cleanup of the current writer's staging file. On Windows with Rust 1.98.1, the pipeline suite passes all 9 tests; both workspace Clippy variants, changed-file formatting and asset policy pass. The unfiltered workspace run is 346 passed, 1 failed, 6 ignored; the same display-deferrals assertion also fails on the base revision (341/1/6). This is integration/regression evidence, not a replacement for the existing PRs or a claim of gameplay improvement. The new test can be adapted into whichever implementation lands. Synthetic inputs only; AI-assisted.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 06:59 UTC · original

Automated triage for the maintainers.

Confirmed on main: Cache::get_or_convert in crates/victory-pipeline/src/cache.rs returns the rename error whenever publishing the staging file fails, even when another worker has just published the same content-addressed entry; the comments reproduce the Windows sharing error with two processes. Open PR #1324 (mgunes1311-lab) carries the same fix (accept the existing entry). Pull requests are open to everyone, so if your branch differs, please compare with #1324 and comment there or open your own.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 06:59 UTC · [original](https://github.com/Marvisak/VIctory/issues/890#issuecomment-7155362147) <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-24T20:11:29Z at=2026-11-25T06:59:02Z --> Automated triage for the maintainers. Confirmed on `main`: `Cache::get_or_convert` in `crates/victory-pipeline/src/cache.rs` returns the `rename` error whenever publishing the staging file fails, even when another worker has just published the same content-addressed entry; the comments reproduce the Windows sharing error with two processes. Open PR #1324 (mgunes1311-lab) carries the same fix (accept the existing entry). Pull requests are open to everyone, so if your branch differs, please compare with #1324 and comment there or open your own. --- _Generated by [Claude Code](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory#890
No description provided.