GPU/pipeline/library: serialize save() file writes/renames and use unique temp paths #566

Open
AscilCH wants to merge 1 commit from upstream-pr/566 into main
AscilCH commented 2026-11-25 02:17:23 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-25T02:17:23Z.


Problem

In crates/victory-gpu/src/pipeline/library.rs, multiple background pipeline worker threads call library::save(false) while shutdown calls library::save(true) (bypassing the 20 s SAVE_EVERY gate). save() released library.saving.write() immediately after serialise(), before calling replace_library(dir, &tmp) and write_atomically(&dir.join(EARLY_FILE), ...), and both operations used fixed .tmp filenames (host-pipelines.tmp / early-pipelines.tmp).

If a second caller entered save() while the first was still renaming host-pipelines.tmp or writing/renaming early-pipelines.tmp, the second caller truncated the same .tmp file mid-rename (Mapped::create / std::fs::write), causing rename failures or replacing host-pipelines.bin / early-pipelines.txt with truncated/corrupt data. Additionally, if serialise() failed, USED_CHANGED had already been cleared to false.

Fix

  • Add a dedicated SAVE_LOCK mutex in library::save() spanning the full file-write and rename sequence while keeping library.saving.write() scoped to GetSerializedSize() + serialise().
  • Use unique per-call temporary filenames (temp_path(), keyed by PID and monotonic counter) and remove the temp file if writing or renaming fails.
  • Restore USED_CHANGED if serialise() fails.
  • Add synthetic unit test temp_paths_are_unique_and_write_atomically_cleans_up.

Verification

  • cargo test -p victory-gpu --locked pipeline::library::tests -- --test-threads=1: 5 passed, 0 failed
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings: clean
  • cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings: clean

(AI-assisted; synthetic unit tests only, no game data.)

<!-- greyfinch-sync:fxceless/victory-fork:issue:6595633731 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/566). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-25T02:17:23Z. --- ## Problem In `crates/victory-gpu/src/pipeline/library.rs`, multiple background pipeline worker threads call `library::save(false)` while shutdown calls `library::save(true)` (bypassing the 20 s `SAVE_EVERY` gate). `save()` released `library.saving.write()` immediately after `serialise()`, **before** calling `replace_library(dir, &tmp)` and `write_atomically(&dir.join(EARLY_FILE), ...)`, and both operations used fixed `.tmp` filenames (`host-pipelines.tmp` / `early-pipelines.tmp`). If a second caller entered `save()` while the first was still renaming `host-pipelines.tmp` or writing/renaming `early-pipelines.tmp`, the second caller truncated the same `.tmp` file mid-rename (`Mapped::create` / `std::fs::write`), causing rename failures or replacing `host-pipelines.bin` / `early-pipelines.txt` with truncated/corrupt data. Additionally, if `serialise()` failed, `USED_CHANGED` had already been cleared to `false`. ## Fix - Add a dedicated `SAVE_LOCK` mutex in `library::save()` spanning the full file-write and rename sequence while keeping `library.saving.write()` scoped to `GetSerializedSize()` + `serialise()`. - Use unique per-call temporary filenames (`temp_path()`, keyed by PID and monotonic counter) and remove the temp file if writing or renaming fails. - Restore `USED_CHANGED` if `serialise()` fails. - Add synthetic unit test `temp_paths_are_unique_and_write_atomically_cleans_up`. ## Verification - `cargo test -p victory-gpu --locked pipeline::library::tests -- --test-threads=1`: 5 passed, 0 failed - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings`: clean - `cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings`: clean *(AI-assisted; synthetic unit tests only, no game data.)*
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/566:upstream-pr/566'
git switch 'upstream-pr/566'

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/566'
git switch 'upstream-pr/566'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/566'
git switch 'upstream-pr/566'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/566'
git switch 'main'
git merge --squash 'upstream-pr/566'
git switch 'main'
git merge --ff-only 'upstream-pr/566'
git switch 'main'
git merge 'upstream-pr/566'
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!566
No description provided.