Skip to content

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

Open
HailLucifer666 wants to merge 2 commits into
fxceless:mainfrom
HailLucifer666:fix/pipeline-library-save-race
Open

HailLucifer666 wants to merge 2 commits into
fxceless:mainfrom
HailLucifer666:fix/pipeline-library-save-race

Conversation

@HailLucifer666

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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.

RicerBoy added a commit to RicerBoy/DropItLikeItsHot that referenced this pull request Nov 24, 2026
…line library saves and use unique temp files

Part of #16

# Conflicts:
#	crates/victory-gpu/src/pipeline/library.rs
@Headache3318

Copy link
Copy Markdown

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 and others added 2 commits November 25, 2026 14:56
THE BUG.
save() staged the pipeline library to a fixed host-pipelines.tmp path and dropped library.saving.write() before calling replace_library(), while write_atomically() staged early-pipelines.txt to a fixed .tmp path with no lock.

WHY NOT HYPOTHETICAL.
Periodic 20 s worker saves and shutdown save(true) can overlap on Windows, causing a concurrent thread to truncate or overwrite the staging file while another thread renames it over the mapped library or early list.

THE FIX.
Serialize on-disk staging and replacement under SAVE_IO (without extending library.saving.write() across renames), generate unique .tmp-<pid>-<seq> staging paths via unique_tmp() for both save() and write_atomically(), and remove the temp file on rename failure.

SCOPE.
Only pipeline/library.rs on-disk staging and replacement. Pipeline compilation, mapping, and cache format are unchanged.
…o write

THE BUG. 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. When a rename failed (the target
held by a folder, the file held open, a system refusing to replace a mapped
file), the retry found nothing dirty and returned: the pipelines this run had
added, and the title pipelines it had noted for an early start, were lost with
the process.

WHY NOT HYPOTHETICAL. avt0m4t48 reproduced it on baf1154 and on main
(2119b0e): with the obstruction removed, a second save(true) returned without
writing the pending data. Both destinations replace_library() tries have to
fail for the library to be lost, but the early list is one write and fails on
its own.

THE FIX. publish() puts the library and the early list in place and clears
the dirty state for exactly what reached the disk: the added count is
acknowledged with fetch_sub(snapshot) only after the rename succeeds, so
stores that arrived while the library was being serialised stay pending, and
a write that did not land sets the early list's flag again. A serialise
failure restores it too.

SCOPE. The rest of the save path is unchanged: the same lock, the same unique
staging file, the same order. The startup ordering change in fxceless#211 is not
part of this.

Three tests, three probes: 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.

Co-authored-by: avt0m4t48 <avt0m4t48@users.noreply.github.com>
@HailLucifer666
HailLucifer666 force-pushed the fix/pipeline-library-save-race branch from baf1154 to f3230ff Compare November 25, 2026 09:32
@HailLucifer666

Copy link
Copy Markdown
Author

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

Copy link
Copy Markdown
Author

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.

Revens2 pushed a commit to Revens2/VItoriaBr that referenced this pull request Nov 25, 2026
…less/victory-fork#164)

(cherry picked from commit 79c97321b45bf2d3635eb4e48280df3fcb5cf1b8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants