D3D12-LOADER-PUBLISH-CONCURRENT-RENAME: tolerate Windows AccessDenied when destination already matches #2675

Merged
leonida/mandy
bobcatchris merged 1 commit from fix/d3d12-loader-publish-concurrent-rename into main 2026-11-30 16:23:21 +00:00

Pull request successfully merged and closed

bobcatchris commented 2026-11-30 01:44:02 +00:00

Claimed in #2570 as D3D12-LOADER-PUBLISH-CONCURRENT-RENAME (#issuecomment-7261851356).

What and why

In d3d12/crates/leonida-loader/src/publish.rs, write_if_changed(path, bytes) stages into a per-process/per-call temp file and publishes via std::fs::rename(&staging, path). On Windows, if a peer runner has already mapped/opened path (e.g. without FILE_SHARE_DELETE), std::fs::rename fails with ERROR_ACCESS_DENIED (os error 5 / PermissionDenied) even when path already holds the exact bytes being published.

This change extracts publish_staging(), cleans up &staging, and returns Ok(()) when std::fs::rename fails and std::fs::read(path) already equals bytes, while still returning Err when the destination is missing or holds different bytes.

Verification (Red -> Green)

Hardware / OS: native Windows 11 Pro (10.0.26200), AMD Ryzen 7 7800X3D, Rust 1.98.

  • Red (before fix): rename_denied_succeeds_when_destination_already_matches_bytes failed deterministically with Os { code: 5, kind: PermissionDenied, message: "Access is denied." }.
  • Green (after fix, rebased onto main 40e7fe69d):
    • cargo test -p leonida-loader --lib publish::tests — 4 passed; 0 failed (75/75 full leonida-loader --lib PASS).
    • python -I scripts/check_no_game_data.py d3d12/crates/leonida-loader/src/publish.rs — PASS.

AI-assisted: Antigravity (Gemini 4 Neon)

Claimed in #2570 as `D3D12-LOADER-PUBLISH-CONCURRENT-RENAME` (`#issuecomment-7261851356`). ### What and why In `d3d12/crates/leonida-loader/src/publish.rs`, `write_if_changed(path, bytes)` stages into a per-process/per-call temp file and publishes via `std::fs::rename(&staging, path)`. On Windows, if a peer runner has already mapped/opened `path` (e.g. without `FILE_SHARE_DELETE`), `std::fs::rename` fails with `ERROR_ACCESS_DENIED` (`os error 5` / `PermissionDenied`) even when `path` already holds the exact `bytes` being published. This change extracts `publish_staging()`, cleans up `&staging`, and returns `Ok(())` when `std::fs::rename` fails and `std::fs::read(path)` already equals `bytes`, while still returning `Err` when the destination is missing or holds different bytes. ### Verification (Red -> Green) Hardware / OS: native Windows 11 Pro (`10.0.26200`), AMD Ryzen 7 7800X3D, Rust `1.98`. - **Red (before fix):** `rename_denied_succeeds_when_destination_already_matches_bytes` failed deterministically with `Os { code: 5, kind: PermissionDenied, message: "Access is denied." }`. - **Green (after fix, rebased onto `main` `40e7fe69d`):** - `cargo test -p leonida-loader --lib publish::tests` — **`4 passed; 0 failed`** (`75/75` full `leonida-loader --lib` PASS). - `python -I scripts/check_no_game_data.py d3d12/crates/leonida-loader/src/publish.rs` — **PASS**. AI-assisted: Antigravity (Gemini 4 Neon)
imShangLi commented 2026-11-30 02:14:54 +00:00

Static review: no blocker found.

  • publish_staging keeps the original rename error unless the destination already contains exactly bytes, which is the correct idempotent outcome.
  • Reading the destination after the failed rename closes the race where a peer publishes the same bytes between the failed rename and the check.
  • Staging cleanup happens on both success-via-existing-destination and error paths.
  • The Windows tests cover matching and mismatching destinations under a no-delete-share lock.

Optional follow-up: the claim mentions concurrent writers, while the tests simulate a locked destination rather than two publishers racing. A small barrier-synchronized multi-writer stress test would strengthen coverage, but I do not see it as a merge blocker.

I did not execute the Rust tests locally. AI-assisted: yes.

Static review: no blocker found. - `publish_staging` keeps the original rename error unless the destination already contains exactly `bytes`, which is the correct idempotent outcome. - Reading the destination after the failed rename closes the race where a peer publishes the same bytes between the failed rename and the check. - Staging cleanup happens on both success-via-existing-destination and error paths. - The Windows tests cover matching and mismatching destinations under a no-delete-share lock. Optional follow-up: the claim mentions concurrent writers, while the tests simulate a locked destination rather than two publishers racing. A small barrier-synchronized multi-writer stress test would strengthen coverage, but I do not see it as a merge blocker. I did not execute the Rust tests locally. AI-assisted: yes.
Kal0s-design commented 2026-11-30 12:30:00 +00:00

Independent Windows verification of d29dfbc49 on main 497bdd039 (clean merge, 0f93f4573). Native Windows 11, MSVC x64, Rust 1.98.1, cargo test -p leonida-loader --lib.

  • Red: main + this PR's two tests, with publish_staging holding main's unchanged rename-or-error step (the function doesn't exist on main). rename_denied_succeeds_when_destination_already_matches_bytes fails with Os { code: 5, kind: PermissionDenied }; the different-bytes test passes, as it should either way.
  • Green: main + this PR. All 4 publish::tests pass; whole crate 83 passed, 0 failed (main: 81 + these two).

On imShangLi's follow-up (a share-mode lock vs. what a peer really holds), two local probes on the merged tree (not part of the PR), each with same = destination already holds the bytes being published:

destination held as plain fs::rename publish_staging (same) publish_staging (different)
loaded as an image (LoadLibraryExW LOAD_LIBRARY_AS_IMAGE_RESOURCE on a copied system DLL, no handle left open, nothing run) error 5 Ok, staging removed error 5, staging removed
a read-only data view (CreateFileMappingW + MapViewOfFile, file opened with share read+delete, handle closed) succeeds Ok Ok (replaced)

So the case the PR is for is real without any share-mode lock: a published file that another runner has loaded as an image can't be replaced, and the PR turns that into success exactly when the bytes already match. A file only mapped as data is replaced normally, so nothing changes there.

GPUs tested: n/a (CPU/file-system tests). AI-assisted: yes.

**Independent Windows verification** of `d29dfbc49` on `main` `497bdd039` (clean merge, `0f93f4573`). Native Windows 11, MSVC x64, Rust 1.98.1, `cargo test -p leonida-loader --lib`. - Red: `main` + this PR's two tests, with `publish_staging` holding `main`'s unchanged rename-or-error step (the function doesn't exist on `main`). `rename_denied_succeeds_when_destination_already_matches_bytes` fails with `Os { code: 5, kind: PermissionDenied }`; the different-bytes test passes, as it should either way. - Green: `main` + this PR. All 4 `publish::tests` pass; whole crate 83 passed, 0 failed (`main`: 81 + these two). On imShangLi's follow-up (a share-mode lock vs. what a peer really holds), two local probes on the merged tree (not part of the PR), each with `same` = destination already holds the bytes being published: | destination held as | plain `fs::rename` | `publish_staging` (same) | `publish_staging` (different) | |---|---|---|---| | loaded as an image (`LoadLibraryExW` `LOAD_LIBRARY_AS_IMAGE_RESOURCE` on a copied system DLL, no handle left open, nothing run) | error 5 | `Ok`, staging removed | error 5, staging removed | | a read-only data view (`CreateFileMappingW` + `MapViewOfFile`, file opened with share read+delete, handle closed) | succeeds | `Ok` | `Ok` (replaced) | So the case the PR is for is real without any share-mode lock: a published file that another runner has loaded as an image can't be replaced, and the PR turns that into success exactly when the bytes already match. A file only mapped as data is replaced normally, so nothing changes there. GPUs tested: n/a (CPU/file-system tests). AI-assisted: yes.
merzleckeier commented 2026-11-30 13:14:14 +00:00

Independent three-process publication test under Wine for d29dfbc4. I used GitHub’s merge b1bde49e and its base d367675c, with Windows x64 MSVC-target Rust 1.98.1 on Wine 11.22 (CachyOS x86-64).

I tested the public write_if_changed entry point. The only local change in both builds is the same checkpoint just after the staging write, before the rename. Three distinct processes write to the same initially absent destination. The controller verifies all three PID-specific staging files and their payloads before letting the first writer publish. It holds the winning file open with FILE_SHARE_READ (no delete sharing), then lets the two other writers continue. One staged the same bytes and the other staged different bytes.

MEASURED (6 rounds per revision)

Writer while the destination is held open Main PR
Same bytes 6/6 PermissionDenied (OS error 5) 6/6 Ok(())
Different bytes 6/6 PermissionDenied (OS error 5) 6/6 PermissionDenied (OS error 5)

In all 12 rounds the first writer succeeded, its bytes survived while the handle was held, no staging files remained after the writers exited, and a different-byte publication succeeded after the handle closed. A second agent recomputed the counts from the raw logs. Synthetic CPU-only test, AI-assisted.

**Independent three-process publication test under Wine** for `d29dfbc4`. I used GitHub’s merge `b1bde49e` and its base `d367675c`, with Windows x64 MSVC-target Rust 1.98.1 on Wine 11.22 (CachyOS x86-64). I tested the public `write_if_changed` entry point. The only local change in both builds is the same checkpoint just after the staging write, before the rename. Three distinct processes write to the same initially absent destination. The controller verifies all three PID-specific staging files and their payloads before letting the first writer publish. It holds the winning file open with `FILE_SHARE_READ` (no delete sharing), then lets the two other writers continue. One staged the same bytes and the other staged different bytes. **MEASURED (6 rounds per revision)** | Writer while the destination is held open | Main | PR | | --- | --- | --- | | Same bytes | 6/6 `PermissionDenied` (OS error 5) | 6/6 `Ok(())` | | Different bytes | 6/6 `PermissionDenied` (OS error 5) | 6/6 `PermissionDenied` (OS error 5) | In all 12 rounds the first writer succeeded, its bytes survived while the handle was held, no staging files remained after the writers exited, and a different-byte publication succeeded after the handle closed. A second agent recomputed the counts from the raw logs. Synthetic CPU-only test, AI-assisted.
MoonEyes commented 2026-11-30 16:23:41 +00:00

Merged on head d29dfbc49. @Kal0s-design ran the red/green on this head (Windows 11, Rust 1.98.1); @merzleckeier checked three concurrent publishers under Wine on this head. CPU and file-system tests, no GPU. CI was green on this head and no test was run by the merger.

Merged by MoonEyes. AI-assisted: yes (Claude Code).

Merged on head `d29dfbc49`. @Kal0s-design ran the red/green on this head (Windows 11, Rust 1.98.1); @merzleckeier checked three concurrent publishers under Wine on this head. CPU and file-system tests, no GPU. CI was green on this head and no test was run by the merger. Merged by MoonEyes. AI-assisted: yes (Claude Code).
Sign in to join this conversation.
No description provided.