GPU: serialize pipeline library saves and use unique temp files #164

Open
HailLucifer666 wants to merge 2 commits from upstream-pr/164 into main
HailLucifer666 commented 2026-11-24 22:06:07 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by HailLucifer666 on 2026-11-24T22:06:07Z.


Problem

  • save() staged the pipeline library to a fixed host-pipelines.tmp path and released library.saving.write() before calling replace_library().
  • write_atomically() staged early-pipelines.txt to a fixed .tmp path with no lock.
  • Overlapping periodic 20 s worker saves and shutdown save(true) can race on the same temp file or leave stale temp files on rename error.

Fix

  • Serialize on-disk staging and replacement under SAVE_IO without holding library.saving.write() across renames.
  • Generate unique .tmp-<pid>-<seq> staging paths via unique_tmp() for both save() and write_atomically().
  • Remove the temp file on error in both replace_library() and write_atomically().

Verification

  • Added multi-threaded tests for both write_atomically and SAVE_IO + replace_library verifying integrity and zero leftover .tmp-* files.
  • cargo clippy --workspace --all-targets --locked -- -D warnings
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
  • cargo test -p victory-gpu pipeline::library::tests --locked (6/6 passed)
  • cargo test -p xtask --locked (4/4 passed)
<!-- greyfinch-sync:fxceless/victory-fork:issue:5481118490 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/164). Originally posted by [HailLucifer666](https://github.com/HailLucifer666) on 2026-11-24T22:06:07Z. --- ### Problem - `save()` staged the pipeline library to a fixed `host-pipelines.tmp` path and released `library.saving.write()` before calling `replace_library()`. - `write_atomically()` staged `early-pipelines.txt` to a fixed `.tmp` path with no lock. - Overlapping periodic 20 s worker saves and shutdown `save(true)` can race on the same temp file or leave stale temp files on rename error. ### Fix - Serialize on-disk staging and replacement under `SAVE_IO` without holding `library.saving.write()` across renames. - Generate unique `.tmp-<pid>-<seq>` staging paths via `unique_tmp()` for both `save()` and `write_atomically()`. - Remove the temp file on error in both `replace_library()` and `write_atomically()`. ### Verification - Added multi-threaded tests for both `write_atomically` and `SAVE_IO` + `replace_library` verifying integrity and zero leftover `.tmp-*` files. - `cargo clippy --workspace --all-targets --locked -- -D warnings` - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` - `cargo test -p victory-gpu pipeline::library::tests --locked` (6/6 passed) - `cargo test -p xtask --locked` (4/4 passed)
avt0m4t48 commented 2026-11-24 23:09:40 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by avt0m4t48 on 2026-11-24T22:58:52Z.


I reproduced a save-retry problem on this head (baf1154) and current main (2119b0e). When an early-list rename fails, or both host-library rename destinations fail, save(true) can clear the dirty state before publication succeeds. After removing the filesystem obstruction, a second save(true) can return without writing the previously pending data. The same problem is present on #211.

An independent retry patch on this head passed eight source-built cases. These cover real rename failures, retrying the early list independently after successful host publication, injected serialize and staging failures, successful fallback, and Stores or early notes arriving during publication. The patch acknowledges the serialized Store count only after a successful rename and subtracts that snapshot so new Stores stay pending. It also restores the early-list dirty flag if its write fails.

The repro uses real filesystem operations with a simulated D3D12 library. Native D3D12 and concurrent processes sharing the same cache were outside this run. This retry behavior can be tested independently of the startup ordering change in #211.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7146870982 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/164#issuecomment-7146870982). Originally posted by [avt0m4t48](https://github.com/avt0m4t48) on 2026-11-24T22:58:52Z. --- I reproduced a save-retry problem on this head (`baf1154`) and current main (`2119b0e`). When an early-list rename fails, or both host-library rename destinations fail, `save(true)` can clear the dirty state before publication succeeds. After removing the filesystem obstruction, a second `save(true)` can return without writing the previously pending data. The same problem is present on #211. An independent retry patch on this head passed eight source-built cases. These cover real rename failures, retrying the early list independently after successful host publication, injected serialize and staging failures, successful fallback, and Stores or early notes arriving during publication. The patch acknowledges the serialized Store count only after a successful rename and subtracts that snapshot so new Stores stay pending. It also restores the early-list dirty flag if its write fails. The repro uses real filesystem operations with a simulated D3D12 library. Native D3D12 and concurrent processes sharing the same cache were outside this run. This retry behavior can be tested independently of the startup ordering change in #211.
Headache3318 commented 2026-11-25 10:11:11 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Headache3318 on 2026-11-25T09:24:15Z.


Independent verification: PR #535 does not exist on this repo (redirects to issue #535). Verified PR #164 (GPU: serialize pipeline library saves and use unique temp files) instead, which matches the description of fixing a race in the pipeline library cache. Build: success. PR-specific tests: 6/6 passed including the 2 new concurrent safety tests (concurrent_write_atomically_never_fails_or_leaves_temp_files, concurrent_save_io_and_replace_library_never_collide_or_leak_tmp). Full workspace tests timed out after 480s (expected for cross-compilation through Wine). No merge conflicts. Machine: Arch Linux, kernel 7.3.5-arch1-1, 3x RTX 4090 driver 620.73.04, Proton, AI-assisted.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7158283253 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/164#issuecomment-7158283253). Originally posted by [Headache3318](https://github.com/Headache3318) on 2026-11-25T09:24:15Z. --- Independent verification: PR #535 does not exist on this repo (redirects to issue #535). Verified PR #164 (GPU: serialize pipeline library saves and use unique temp files) instead, which matches the description of fixing a race in the pipeline library cache. Build: success. PR-specific tests: 6/6 passed including the 2 new concurrent safety tests (concurrent_write_atomically_never_fails_or_leaves_temp_files, concurrent_save_io_and_replace_library_never_collide_or_leak_tmp). Full workspace tests timed out after 480s (expected for cross-compilation through Wine). No merge conflicts. Machine: Arch Linux, kernel 7.3.5-arch1-1, 3x RTX 4090 driver 620.73.04, Proton, AI-assisted.
HailLucifer666 commented 2026-11-25 10:11:14 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by HailLucifer666 on 2026-11-25T09:44:40Z.


Thank you — this reproduced exactly as you described, and the fix follows the shape of your patch.

What was wrong. save() cleared both dirty flags before either write landed: it stored 0 into the library's added count before replace_library() renamed the serialised bytes into place, and it cleared the early list's changed flag before write_atomically() wrote that list. A failed rename therefore left the retry with nothing to do. Both destinations have to fail for the library to be lost, but the early list is one write and fails on its own — including when the library write fails first and the list is never attempted.

What changed (crates/victory-gpu/src/pipeline/library.rs). The publication step is now publish():

  • added.fetch_sub(snapshot, Relaxed) only after replace_library() returns Ok(()), so stores that arrive while the library is being serialised stay pending for the next save.
  • A write that did not land sets the early list's flag again, so the next save retries it, independently of the library.
  • A serialise() failure restores the early-list flag too; that path used to drop the pending early list as well.

Tests — three, new, all with real filesystem operations against a simulated library, as in your repro:

  • a_save_whose_rename_fails_keeps_its_stores_and_early_list_pending — both rename destinations held by a folder (ERROR_ACCESS_DENIED), then released: the retry writes both files.
  • a_save_acknowledges_only_the_stores_it_serialised — a store arriving between the snapshot and the publication stays pending.
  • an_early_list_that_cannot_be_written_stays_pending_after_the_library_is_published — the library lands, the list does not, and only the list stays dirty.

Each was checked against the bug: acknowledging the stores before the rename, clearing every store instead of the snapshot, and dropping the early-list restore each fail the test named for it. The probes are recorded in the test file so the next editor has them.

Gate, on this head rebased onto 9376fd60: pipeline::library::tests 9 passed; cargo test -p victory-gpu 149 passed, 3 ignored; cargo test -p xtask 4 passed. Both clippy passes are red in this workspace for three pre-existing diagnostics that are identical on main at 9376fd60 (placed_filler_twins never used, sort_unstable_by_key in draw.rs, depth_written never read) — main's own CI Clippy job failed at that commit. This branch adds none of them; I left them alone rather than widen the diff.

Not covered, the same as your run: native D3D12 and concurrent processes sharing one cache. No local title files here, so it is the same simulated-library shape you used.

Rebased onto 9376fd60 and force-pushed with lease.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7158741163 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/164#issuecomment-7158741163). Originally posted by [HailLucifer666](https://github.com/HailLucifer666) on 2026-11-25T09:44:40Z. --- Thank you — this reproduced exactly as you described, and the fix follows the shape of your patch. **What was wrong.** `save()` cleared both dirty flags before either write landed: it stored `0` into the library's `added` count before `replace_library()` renamed the serialised bytes into place, and it cleared the early list's `changed` flag before `write_atomically()` wrote that list. A failed rename therefore left the retry with nothing to do. Both destinations have to fail for the library to be lost, but the early list is one write and fails on its own — including when the library write fails first and the list is never attempted. **What changed** (`crates/victory-gpu/src/pipeline/library.rs`). The publication step is now `publish()`: - `added.fetch_sub(snapshot, Relaxed)` only after `replace_library()` returns `Ok(())`, so stores that arrive while the library is being serialised stay pending for the next save. - A write that did not land sets the early list's flag again, so the next save retries it, independently of the library. - A `serialise()` failure restores the early-list flag too; that path used to drop the pending early list as well. **Tests** — three, new, all with real filesystem operations against a simulated library, as in your repro: - `a_save_whose_rename_fails_keeps_its_stores_and_early_list_pending` — both rename destinations held by a folder (`ERROR_ACCESS_DENIED`), then released: the retry writes both files. - `a_save_acknowledges_only_the_stores_it_serialised` — a store arriving between the snapshot and the publication stays pending. - `an_early_list_that_cannot_be_written_stays_pending_after_the_library_is_published` — the library lands, the list does not, and only the list stays dirty. Each was checked against the bug: acknowledging the stores before the rename, clearing every store instead of the snapshot, and dropping the early-list restore each fail the test named for it. The probes are recorded in the test file so the next editor has them. **Gate**, on this head rebased onto `9376fd60`: `pipeline::library::tests` 9 passed; `cargo test -p victory-gpu` 149 passed, 3 ignored; `cargo test -p xtask` 4 passed. Both clippy passes are red in this workspace for three pre-existing diagnostics that are identical on `main` at `9376fd60` (`placed_filler_twins` never used, `sort_unstable_by_key` in `draw.rs`, `depth_written` never read) — `main`'s own CI Clippy job failed at that commit. This branch adds none of them; I left them alone rather than widen the diff. **Not covered**, the same as your run: native D3D12 and concurrent processes sharing one cache. No local title files here, so it is the same simulated-library shape you used. Rebased onto `9376fd60` and force-pushed with lease.
HailLucifer666 commented 2026-11-25 14:10:51 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by HailLucifer666 on 2026-11-25T13:49:16Z.


Your repro is addressed on f3230ff5, and I want to be precise about which of your requirements each part answers, because the ordering is the whole fix.

publish() now does, in this order:

  1. Serialise to a unique temp file, then replace_library — the rename, or the fallback that leaves the new file beside the old one.
  2. Only if that returned Ok: added.fetch_sub(snapshot), where snapshot is the count taken before serialisation began. That is your "acknowledge the serialized Store count only after a successful rename".
  3. Subtracting the snapshot rather than zeroing the counter is your "subtract that snapshot so new Stores stay pending" — stores that arrive during the write are not in snapshot, so they survive the subtraction and the next save writes them.
  4. Whatever happened to the library, the early list is written; if it fails, or was never attempted because the library rename failed, changed is set back to true.

So the failure path leaves the store count untouched and the early list dirty, and the next save(true) writes the data instead of returning early. That is the bug you described at baf1154.

Tests, one per requirement — none of these existed before:

test what it pins
a_save_acknowledges_only_the_stores_it_serialised the subtraction is by snapshot, not by zeroing
a_save_whose_rename_fails_keeps_its_stores_and_early_list_pending a failed rename leaves the count at the pre-save value
an_early_list_that_cannot_be_written_stays_pending_after_the_library_is_published the early list's dirty flag is restored

Probe: acknowledge the stores before checking the rename result — the shape of the original bug — and all three fail, the first with the store that arrived during the save is still pending (count -3, i.e. an underflow past zero, which is what acknowledges-what-never-wrote looks like). Restored byte-identical afterwards.

cargo test -p victory-gpu --lib pipeline::library: 9 passed, 0 failed.

Two things I did not cover, so they are not read as covered: native D3D12 — the tests use a simulated library, as you noted — and two processes sharing one cache directory. Both are outside what this repo can run here. If you have a machine where the obstruction is real (a mapped file the system refuses to replace), that is the run that would settle replace_library's fallback path rather than its error path.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7164338294 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/164#issuecomment-7164338294). Originally posted by [HailLucifer666](https://github.com/HailLucifer666) on 2026-11-25T13:49:16Z. --- Your repro is addressed on `f3230ff5`, and I want to be precise about which of your requirements each part answers, because the ordering is the whole fix. **`publish()`** now does, in this order: 1. Serialise to a unique temp file, then `replace_library` — the rename, or the fallback that leaves the new file beside the old one. 2. **Only if that returned `Ok`**: `added.fetch_sub(snapshot)`, where `snapshot` is the count taken before serialisation began. That is your "acknowledge the serialized Store count only after a successful rename". 3. **Subtracting the snapshot rather than zeroing the counter** is your "subtract that snapshot so new Stores stay pending" — stores that arrive during the write are not in `snapshot`, so they survive the subtraction and the next save writes them. 4. Whatever happened to the library, the early list is written; if it fails, or was never attempted because the library rename failed, `changed` is set back to `true`. So the failure path leaves the store count untouched and the early list dirty, and the next `save(true)` writes the data instead of returning early. That is the bug you described at `baf1154`. **Tests, one per requirement** — none of these existed before: | test | what it pins | |---|---| | `a_save_acknowledges_only_the_stores_it_serialised` | the subtraction is by snapshot, not by zeroing | | `a_save_whose_rename_fails_keeps_its_stores_and_early_list_pending` | a failed rename leaves the count at the pre-save value | | `an_early_list_that_cannot_be_written_stays_pending_after_the_library_is_published` | the early list's dirty flag is restored | **Probe**: acknowledge the stores *before* checking the rename result — the shape of the original bug — and all three fail, the first with `the store that arrived during the save is still pending` (count `-3`, i.e. an underflow past zero, which is what acknowledges-what-never-wrote looks like). Restored byte-identical afterwards. `cargo test -p victory-gpu --lib pipeline::library`: 9 passed, 0 failed. Two things I did **not** cover, so they are not read as covered: native D3D12 — the tests use a simulated library, as you noted — and two processes sharing one cache directory. Both are outside what this repo can run here. If you have a machine where the obstruction is real (a mapped file the system refuses to replace), that is the run that would settle `replace_library`'s fallback path rather than its error path.
Headache3318 commented 2026-11-30 12:41:22 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Headache3318 on 2026-11-30T12:41:22Z.


Independent verification at head f3230ff5 (AI-assisted, Hermes Agent; Arch Linux, kernel 7.3.6-arch1-2, 3x RTX 4090 driver 620.73.04 — the tests cross-compile x86_64-pc-windows-msvc with cargo-xwin and run under wine; no game data, no native D3D12).

The three new tests and the full library suite pass on a second host. cargo xwin test -p victory-gpu --lib pipeline::library --target x86_64-pc-windows-msvc at f3230ff5ce8c: 9 passed / 0 failed (exit 0), including all three you list pinning the repro — a_save_acknowledges_only_the_stores_it_serialised, a_save_whose_rename_fails_keeps_its_stores_and_early_list_pending, an_early_list_that_cannot_be_written_stays_pending_after_the_library_is_published — plus the two concurrency tests from my earlier check on the previous head. Your mapping of requirements to tests holds as far as these runs can see.

On the obstruction run you asked for: on Linux/wine the rename obstruction fires without needing a mapped file (the destination held by a directory fails the rename as EISDIR), so the error path is now confirmed on a second host too — but that means this box cannot tell which errno its test branch took, and the fallback path (the new file left beside the old one) still has no second-host run from here. Agree it needs a native run to settle.

One host note, not caused by this PR: run natively on Linux, the same command fails to compile with three errors (crate::texture, crate::host, host_resource configured out — list.rs:1809, list.rs:1815, resource.rs:619). The unmodified base 9376fd6 fails with the identical three, so it is pre-existing cfg gating on main of that era; this PR touches only pipeline/library.rs, so the Windows-target run above is the right way to exercise it here.

Mergeability — needs author action: main's history has been rewritten since this branch was based. There is no merge base between the PR (at its base 9376fd6) and current main 368967f (git merge-base returns nothing; GitHub shows CONFLICTING), so a plain rebase cannot work. Simulated the fresh port: git cherry-pick cf0de55 f3230ff5 onto 368967f conflicts in exactly one file, crates/victory-gpu/src/pipeline/library.rs — main has since moved that file — everything else applies clean. Resolving that one file should carry the fix over intact.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7278786330 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/164#issuecomment-7278786330). Originally posted by [Headache3318](https://github.com/Headache3318) on 2026-11-30T12:41:22Z. --- Independent verification at head `f3230ff5` (AI-assisted, Hermes Agent; Arch Linux, kernel 7.3.6-arch1-2, 3x RTX 4090 driver 620.73.04 — the tests cross-compile `x86_64-pc-windows-msvc` with `cargo-xwin` and run under wine; no game data, no native D3D12). **The three new tests and the full library suite pass on a second host.** `cargo xwin test -p victory-gpu --lib pipeline::library --target x86_64-pc-windows-msvc` at `f3230ff5ce8c`: **9 passed / 0 failed** (exit 0), including all three you list pinning the repro — `a_save_acknowledges_only_the_stores_it_serialised`, `a_save_whose_rename_fails_keeps_its_stores_and_early_list_pending`, `an_early_list_that_cannot_be_written_stays_pending_after_the_library_is_published` — plus the two concurrency tests from my earlier check on the previous head. Your mapping of requirements to tests holds as far as these runs can see. On the obstruction run you asked for: on Linux/wine the rename obstruction fires without needing a mapped file (the destination held by a directory fails the rename as `EISDIR`), so the **error path is now confirmed on a second host too** — but that means this box cannot tell which errno its test branch took, and the **fallback** path (the new file left beside the old one) still has no second-host run from here. Agree it needs a native run to settle. One host note, **not** caused by this PR: run natively on Linux, the same command fails to compile with three errors (`crate::texture`, `crate::host`, `host_resource` configured out — `list.rs:1809`, `list.rs:1815`, `resource.rs:619`). The unmodified base `9376fd6` fails with the identical three, so it is pre-existing cfg gating on main of that era; this PR touches only `pipeline/library.rs`, so the Windows-target run above is the right way to exercise it here. **Mergeability — needs author action:** `main`'s history has been rewritten since this branch was based. There is no merge base between the PR (at its base `9376fd6`) and current main `368967f` (`git merge-base` returns nothing; GitHub shows CONFLICTING), so a plain rebase cannot work. Simulated the fresh port: `git cherry-pick cf0de55 f3230ff5` onto `368967f` conflicts in exactly one file, `crates/victory-gpu/src/pipeline/library.rs` — main has since moved that file — everything else applies clean. Resolving that one file should carry the fix over intact.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/164:upstream-pr/164'
git switch 'upstream-pr/164'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/164'
git switch 'upstream-pr/164'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/164'
git switch 'upstream-pr/164'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/164'
git switch 'main'
git merge --squash 'upstream-pr/164'
git switch 'main'
git merge --ff-only 'upstream-pr/164'
git switch 'main'
git merge 'upstream-pr/164'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
fxceless/victory-fork!164
No description provided.