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

Open
github-bot wants to merge 1 commit from github-bot/gh-566 into main AGit
Owner

@⁠AscilCH on GitHub · 2026-11-25 02:17 UTC · original
AscilCH:fix/gpu-pipeline-library-save-tmp-race → main · +62 −6 · 1 files · 1 commits · open

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.)

**@⁠AscilCH** on GitHub · 2026-11-25 02:17 UTC · [original](https://github.com/fxceless/victory-fork/pull/566) `AscilCH:fix/gpu-pipeline-library-save-tmp-race` → `main` · +62 −6 · 1 files · 1 commits · open ## 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.)*
- Hold a dedicated `SAVE_LOCK` mutex across the full file-write and rename
  sequence in `library::save()` (keeping `library.saving.write()` tightly
  scoped to `GetSerializedSize` + `serialise`) so concurrent worker saves and
  shutdown's `save(true)` cannot race on `.tmp` truncation and renames.
- Generate unique per-save temporary filenames via `temp_path()` (PID +
  monotonic sequence) and remove the temporary file on write/rename errors.
- Preserve `USED_CHANGED` if `serialise()` fails so pending early-pipeline
  list updates are not dropped.
- Add synthetic unit test `temp_paths_are_unique_and_write_atomically_cleans_up`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:46:30 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/566/head:github-bot/gh-566'
git switch 'github-bot/gh-566'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-566'
git switch 'github-bot/gh-566'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-566'
git switch 'github-bot/gh-566'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-566'
git switch 'main'
git merge --squash 'github-bot/gh-566'
git switch 'main'
git merge --ff-only 'github-bot/gh-566'
git switch 'main'
git merge 'github-bot/gh-566'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.