WIP: fix(fs): preserve orphan staging files so saves can complete #2806

Draft
NullMagic wants to merge 2 commits from NullMagic/Longshot:fix/orphan-save-staging into main
First-time contributor

Summary

A regular 0.longshot-new left in the committed save directory without a recovery journal makes every later Commit fail with 0x2EE202. A manual save then stays on “Saving…” indefinitely, while rendering continues. This reproduced with a player's existing staging-file collision on nightly-920 and the same collision is present in stable v3.2.0.

After recovery has finished, preserve a colliding regular staging file under an unused orphan-<index>-<number>.longshot-new name and sync the directory before staging the new save. The reserved suffix keeps the preserved bytes out of the game's working copy. Pending recovery journals, links and directories remain protected. Existing orphan entries are skipped and a refused rename leaves the original available for retry.

Read save_preserve_orphan() and the new orphan_cases() in tests/test_fs_save_commit.py first. See #607 for the original staging design; #2250 concerns a different mount-cleanup refusal.

Stable backport: the same test-first fix is available on fix/orphan-save-staging-v3.2.0, based on the v3.2.0 tag (79d192c655b5): test 2bf932e, fix c61370b. There is currently no upstream release/v3.2 branch to target with a second PR. No release/tag is changed by this proposal.

Testing

Tested: Ubuntu 26.04.1, Ryzen 7 8700G, Radeon RX 7900 XTX, RADV Mesa 26.0.8.

  • The new regression fails on unchanged mainline and stable sources at orphan staging files must not block every subsequent Commit.
  • The whole save-commit HLE suite passes with GCC and Clang on both mainline and v3.2.0. It compiles the complete nn_fs.c with the real Linux file layer, including crash/journal recovery and process ownership tests.
  • Final cases cover absent/empty/incomplete journals, multiple staging collisions, repeated saves, existing backups, dangling backup links, directories, repeated quarantine refusal and out-of-space retries. The essential-call mutation fails the regression.
  • Rebuilt the release runtime incrementally (2 of 654 units, 0 compile failures). In Zora River, loaded a copied save, opened System → Save, confirmed Save with an intentionally stale file present: it preserved the fixture and returned from Saving to the menu. The saved files were published. The player's original staging collisions were separately moved aside without deleting their bytes.
  • Mainline full strict game-free run: 694 passed, 191 failed, 10 platform skips. Every one of the 191 failing checks also failed on a clean worktree at 01d6878. Most require missing Vulkan/SPIR-V/shader tools or a newer Rust compiler; others are existing fixture/source mismatches and timeouts. Baseline failure rerun used a 30 s timeout; the full run used 120 s. All save-related checks in that full run passed; the Windows-specific path test was skipped.
  • Independent second-agent review: no remaining blocking issues.

Still to test:

  • Windows (CI or by hand): build the host and run the save-commit suite and manual-save collision case; expect a preserved orphan and a successful save.
  • Stable v3.2.0 in-game execution of the backport (by hand); its complete Linux HLE tests pass with both compilers.

Measurements

Verdict: the stale-file collision no longer blocks saving. These are diagnostic observations, not a frame-rate benchmark.

Before: main @ 01d6878 After: this PR @ f6b2e93
Manual save with stale staging file Repeated result 0x2EE202; Saving does not finish Preserved orphan, then successful slot/options commits in 4.899 ms and 4.200 ms
New orphan regression Fails Passes

The change runs only during save publication and adds a staging-path attribute lookup when there is no collision. It changes no per-frame renderer work, shader translator or cache format. No performance gain is claimed. An orphan collision adds a rename and a directory sync; its bytes remain on disk.

Risks

Linux's existing MoveFileExA compatibility function uses a destination check followed by rename. The save ownership lock excludes other Longshot instances, but this patch does not add protection against an external process racing creation of the selected backup path. Windows uses the existing native no-replace move. Windows validation remains pending.

Checklist

Ready for review

  • One topic on current main; Conventional Commit title.
  • Tested on my own PC, in the game for a change the game runs, as Testing says.
  • mise run test passes (any failure that also fails on main is named). The equivalent strict Python runner was used; failures are listed below and all reproduced on unchanged main.
  • Measured against main, or Measurements says why there is nothing to measure.
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.

When it applies

  • A fix: regression fails before and passes after.
  • Existing source-reading tests are retained and extended.
  • Host code builds on Windows: pending CI/by-hand validation; Linux builds and tests passed.
  • Save recovery documentation updated.
  • AI assistance recorded in commit trailers.

Ready to merge

  • Remaining platform/backport game tests completed.
  • Maintainer review and CI accepted.
## Summary A regular `0.longshot-new` left in the committed save directory without a recovery journal makes every later Commit fail with `0x2EE202`. A manual save then stays on “Saving…” indefinitely, while rendering continues. This reproduced with a player's existing staging-file collision on nightly-920 and the same collision is present in stable v3.2.0. After recovery has finished, preserve a colliding regular staging file under an unused `orphan-<index>-<number>.longshot-new` name and sync the directory before staging the new save. The reserved suffix keeps the preserved bytes out of the game's working copy. Pending recovery journals, links and directories remain protected. Existing orphan entries are skipped and a refused rename leaves the original available for retry. Read `save_preserve_orphan()` and the new `orphan_cases()` in `tests/test_fs_save_commit.py` first. See #607 for the original staging design; #2250 concerns a different mount-cleanup refusal. **Stable backport:** the same test-first fix is available on [fix/orphan-save-staging-v3.2.0](https://git.magicbean.dev/NullMagic/Longshot/src/branch/fix%2Forphan-save-staging-v3.2.0), based on the v3.2.0 tag (`79d192c655b5`): test `2bf932e`, fix `c61370b`. There is currently no upstream `release/v3.2` branch to target with a second PR. No release/tag is changed by this proposal. ## Testing **Tested:** Ubuntu 26.04.1, Ryzen 7 8700G, Radeon RX 7900 XTX, RADV Mesa 26.0.8. - The new regression fails on unchanged mainline and stable sources at `orphan staging files must not block every subsequent Commit`. - The whole save-commit HLE suite passes with GCC and Clang on both mainline and v3.2.0. It compiles the complete `nn_fs.c` with the real Linux file layer, including crash/journal recovery and process ownership tests. - Final cases cover absent/empty/incomplete journals, multiple staging collisions, repeated saves, existing backups, dangling backup links, directories, repeated quarantine refusal and out-of-space retries. The essential-call mutation fails the regression. - Rebuilt the release runtime incrementally (2 of 654 units, 0 compile failures). In Zora River, loaded a copied save, opened System → Save, confirmed Save with an intentionally stale file present: it preserved the fixture and returned from Saving to the menu. The saved files were published. The player's original staging collisions were separately moved aside without deleting their bytes. - Mainline full strict game-free run: **694 passed, 191 failed, 10 platform skips**. Every one of the 191 failing checks also failed on a clean worktree at `01d6878`. Most require missing Vulkan/SPIR-V/shader tools or a newer Rust compiler; others are existing fixture/source mismatches and timeouts. Baseline failure rerun used a 30 s timeout; the full run used 120 s. All save-related checks in that full run passed; the Windows-specific path test was skipped. - Independent second-agent review: no remaining blocking issues. **Still to test:** - Windows (CI or by hand): build the host and run the save-commit suite and manual-save collision case; expect a preserved orphan and a successful save. - Stable v3.2.0 in-game execution of the backport (by hand); its complete Linux HLE tests pass with both compilers. ## Measurements **Verdict:** the stale-file collision no longer blocks saving. These are diagnostic observations, not a frame-rate benchmark. | | Before: main @ 01d6878 | After: this PR @ f6b2e93 | |---|---|---| | Manual save with stale staging file | Repeated result `0x2EE202`; Saving does not finish | Preserved orphan, then successful slot/options commits in 4.899 ms and 4.200 ms | | New orphan regression | Fails | Passes | The change runs only during save publication and adds a staging-path attribute lookup when there is no collision. It changes no per-frame renderer work, shader translator or cache format. No performance gain is claimed. An orphan collision adds a rename and a directory sync; its bytes remain on disk. ## Risks Linux's existing `MoveFileExA` compatibility function uses a destination check followed by rename. The save ownership lock excludes other Longshot instances, but this patch does not add protection against an external process racing creation of the selected backup path. Windows uses the existing native no-replace move. Windows validation remains pending. ## Checklist ### Ready for review - [x] One topic on current main; Conventional Commit title. - [x] Tested on my own PC, in the game for a change the game runs, as Testing says. - [x] `mise run test` passes (any failure that also fails on `main` is named). The equivalent strict Python runner was used; failures are listed below and all reproduced on unchanged main. - [x] Measured against `main`, or Measurements says why there is nothing to measure. - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. ### When it applies - [x] A fix: regression fails before and passes after. - [x] Existing source-reading tests are retained and extended. - [ ] Host code builds on Windows: pending CI/by-hand validation; Linux builds and tests passed. - [x] Save recovery documentation updated. - [x] AI assistance recorded in commit trailers. ### Ready to merge - [ ] Remaining platform/backport game tests completed. - [ ] Maintainer review and CI accepted.
Exercise missing/incomplete journals, repeated staging collisions and refused quarantine, preserving all saved bytes. The new orphan cases fail on the unchanged runtime.

Assisted-by: ChatGPT
Co-Authored-By: Codex <[email protected]>
fix(fs): preserve orphan staging files so saves can complete
Some checks are pending
CI / What the change needs (pull_request) Blocked by required conditions
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Blocked by required conditions
CI / Third-party parts and guide screenshots (pull_request) Blocked by required conditions
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Blocked by required conditions
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Blocked by required conditions
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Blocked by required conditions
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Blocked by required conditions
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Blocked by required conditions
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
PR description / Title and description (pull_request_target) Successful in 6s
f6b2e93f4f
After journal recovery finishes, preserve a colliding regular staging file under an unused reserved orphan name, sync the directory and stage the new save. Keep pending journals, links and directories protected. A failed quarantine remains retryable.

Assisted-by: ChatGPT
Co-Authored-By: Codex <[email protected]>
Member

Possibly related, for context:

  • PR #2529 WIP: fix(fs): a create, delete or rename gives the virtual core up on every mount — Open host/nn_fs.c changes in the same filesystem code; review for overlap/conflicts
Possibly related, for context: - PR #2529 WIP: fix(fs): a create, delete or rename gives the virtual core up on every mount — Open host/nn_fs.c changes in the same filesystem code; review for overlap/conflicts
Member
@review
BeanBot left a comment

Recommendation: Request changes. The fix clears the reported save collision on Linux, but its Linux rename can overwrite another preserved file created concurrently. Windows validation is also still pending.

A stale staging file is moved aside so a new save can complete, while its bytes remain available for recovery. The change adds a filesystem lookup to save publication and a rename and directory sync when a collision occurs.

Blocking (1): must change before merging.

  1. L1 host/nn_fs.c:617: On Linux this move does not atomically enforce the unused destination, so it can overwrite a preserved file created by another process. (more on the line) Do: Use an atomic no-replace move for orphan preservation on Linux, and retry another name when the destination already exists.

Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)

  • L2 host/nn_fs.c:617: The Windows save-publication path has not yet been validated. (more on the line) Do: Build the host on Windows and run the save-commit suite and an in-game manual save with a stale staging file, including a UTF-8 save path.

@fix can do L1.

Checks: Still running: 10 checks.

More: does it help, other systems, switches, what was read

Does it help: Yes for the reported Linux case: the PR reports repeated 0x2EE202 results on the base and a successful in-game save with the orphan preserved at this head. The regression test is reported to fail before and pass after. No performance gain is claimed.

Other systems: The change affects staged save publication and recovery on Linux and Windows, independently of GPU. Linux HLE and in-game results are reported; the Windows build and collision case remain untested. The existing ownership lock excludes another Longshot owner but does not exclude external save tools.

Switches: None.

Reviewed: f6b2e93f4f against 01d6878b3f: Read the repository rules, PR template, commits, diff, changed save code and its recovery and filesystem helpers, changed test cases and saves documentation. Skimmed the remainder of the large host and test files and the standards document. docs/review-checklist.md and the adjacent Tooling checkout were absent.; nothing run (a code review).

Checked and fine:

  • Recovery is attempted before new staging is planned, and a pending journal prevents orphan preservation.
  • The regression exercises repeated collisions, existing backups, failed moves and incomplete journals.
  • Both commits contain AI-assistance trailers; no switches, generated files or game-derived files are added.

Testing: No code or tests were run, as requested. The PR reports Linux GCC and Clang HLE runs and an in-game reproduction; CI is still running.

Overlaps and merge order: Not checked; the network is closed.


Automated code review by BeanBot (gpt-6.1-terra), asked for by llun. Nothing was run; a maintainer decides.

**Recommendation: Request changes.** The fix clears the reported save collision on Linux, but its Linux rename can overwrite another preserved file created concurrently. Windows validation is also still pending. A stale staging file is moved aside so a new save can complete, while its bytes remain available for recovery. The change adds a filesystem lookup to save publication and a rename and directory sync when a collision occurs. **Blocking (1)**: must change before merging. 1. **L1** `host/nn_fs.c:617`: On Linux this move does not atomically enforce the unused destination, so it can overwrite a preserved file created by another process. (more on the line) **Do:** Use an atomic no-replace move for orphan preservation on Linux, and retry another name when the destination already exists. **Testing before merging (1)** (runs on the systems it can change; it doesn't hold the recommendation) - **L2** `host/nn_fs.c:617`: The Windows save-publication path has not yet been validated. (more on the line) **Do:** Build the host on Windows and run the save-commit suite and an in-game manual save with a stale staging file, including a UTF-8 save path. `@fix` can do L1. **Checks:** Still running: 10 checks. <details> <summary>More: does it help, other systems, switches, what was read</summary> **Does it help:** Yes for the reported Linux case: the PR reports repeated 0x2EE202 results on the base and a successful in-game save with the orphan preserved at this head. The regression test is reported to fail before and pass after. No performance gain is claimed. **Other systems:** The change affects staged save publication and recovery on Linux and Windows, independently of GPU. Linux HLE and in-game results are reported; the Windows build and collision case remain untested. The existing ownership lock excludes another Longshot owner but does not exclude external save tools. **Switches:** None. **Reviewed:** f6b2e93f4f1b against 01d6878b3f14: Read the repository rules, PR template, commits, diff, changed save code and its recovery and filesystem helpers, changed test cases and saves documentation. Skimmed the remainder of the large host and test files and the standards document. docs/review-checklist.md and the adjacent Tooling checkout were absent.; nothing run (a code review). **Checked and fine:** - Recovery is attempted before new staging is planned, and a pending journal prevents orphan preservation. - The regression exercises repeated collisions, existing backups, failed moves and incomplete journals. - Both commits contain AI-assistance trailers; no switches, generated files or game-derived files are added. **Testing:** No code or tests were run, as requested. The PR reports Linux GCC and Clang HLE runs and an in-game reproduction; CI is still running. **Overlaps and merge order:** Not checked; the network is closed. </details> --- <sub>Automated code review by BeanBot (gpt-6.1-terra), asked for by llun. Nothing was run; a maintainer decides.</sub>
@ -604,0 +614,4 @@
snprintf(name, sizeof name, "orphan-%u-%u.longshot-new", index, i);
if (!save_join(backup, s->committed, name) || !save_attr(backup, &at)) return 0;
if (at == INVALID_FILE_ATTRIBUTES) {
if (!MoveFileExA(stage, backup, MOVEFILE_WRITE_THROUGH) || !save_sync_directory(s->committed)) return 0;
Member

L1 (blocking): On Linux this move does not atomically enforce the unused destination, so it can overwrite a preserved file created by another process.

When: If a save tool creates the selected orphan name after the attribute lookup, the POSIX MoveFileExA compatibility function checks the destination and then calls rename; a creation between its check and rename is replaced, losing that file's bytes.

Do: Use an atomic no-replace move for orphan preservation on Linux, and retry another name when the destination already exists.

**L1** (blocking): On Linux this move does not atomically enforce the unused destination, so it can overwrite a preserved file created by another process. *When:* If a save tool creates the selected orphan name after the attribute lookup, the POSIX MoveFileExA compatibility function checks the destination and then calls rename; a creation between its check and rename is replaced, losing that file's bytes. **Do:** Use an atomic no-replace move for orphan preservation on Linux, and retry another name when the destination already exists.
Member

L2 (should fix): The Windows save-publication path has not yet been validated.

When: A Windows player with a stale staging file relies on the wide-path move and native no-replace behavior to preserve it and finish saving; the PR reports neither the Windows suite nor the manual collision case has run.

Do: Build the host on Windows and run the save-commit suite and an in-game manual save with a stale staging file, including a UTF-8 save path.

**L2** (should fix): The Windows save-publication path has not yet been validated. *When:* A Windows player with a stale staging file relies on the wide-path move and native no-replace behavior to preserve it and finish saving; the PR reports neither the Windows suite nor the manual collision case has run. **Do:** Build the host on Windows and run the save-commit suite and an in-game manual save with a stale staging file, including a UTF-8 save path.
Contributor

Ran into the same bug independently while reading the save code, so some confirmation for this one:

  • On current main (55773e3), a regular 0.longshot-new in the committed folder with no journal still makes the next Commit fail: fs: Commit: 0 files, 1 ops, 0 failed paths, 0 open writers, result 0x2ee202, reproduced game-free in tests/test_fs_save_commit.py's harness on Linux. This branch also still merges onto current main without conflicts.
  • A small one this PR could take along: Save.cleanup is set in save_replay (after deleting an empty journal and after dropping an incomplete one) but never read anywhere. It looks like an earlier hook for exactly this case.
  • For context, one way the orphan gets made: hos_close_finish only waits for open writes (hos_fs_wait_writes, from #1422), and Commit runs with no file open for writing, so closing the window during an autosave can cut a commit between staging and the journal. I'll look at that separately, without touching this PR's code.
Ran into the same bug independently while reading the save code, so some confirmation for this one: - On current main (`55773e3`), a regular `0.longshot-new` in the committed folder with no journal still makes the next Commit fail: `fs: Commit: 0 files, 1 ops, 0 failed paths, 0 open writers, result 0x2ee202`, reproduced game-free in `tests/test_fs_save_commit.py`'s harness on Linux. This branch also still merges onto current main without conflicts. - A small one this PR could take along: `Save.cleanup` is set in `save_replay` (after deleting an empty journal and after dropping an incomplete one) but never read anywhere. It looks like an earlier hook for exactly this case. - For context, one way the orphan gets made: `hos_close_finish` only waits for open writes (`hos_fs_wait_writes`, from #1422), and Commit runs with no file open for writing, so closing the window during an autosave can cut a commit between staging and the journal. I'll look at that separately, without touching this PR's code.
Some checks are pending
CI / What the change needs (pull_request) Blocked by required conditions
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Blocked by required conditions
CI / Third-party parts and guide screenshots (pull_request) Blocked by required conditions
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Blocked by required conditions
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Blocked by required conditions
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Blocked by required conditions
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Blocked by required conditions
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Blocked by required conditions
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
PR description / Title and description (pull_request_target) Successful in 6s
This pull request has changes conflicting with the target branch.
  • docs/user/saves.md
Some workflows are waiting to be reviewed.
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/orphan-save-staging:NullMagic-fix/orphan-save-staging'
git switch 'NullMagic-fix/orphan-save-staging'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
4 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!2806
No description provided.