Skip to content

XGameSave: a failed XGameSaveSubmitUpdate leaves the update half applied #324

In review

Description

@mastrr9

apply in crates/xcl/src/hle/xgameruntime/gamesave.rs (line 564 on main 85bdf21) writes the display name, then each blob in turn, each with its own atomic replace. When one write fails partway, it returns E_FAIL, but the display name and every blob before the failing one are already replaced.

The GDK documents an update as all or nothing: "we want the whole operation to succeed or fail atomically. We don't want to have a partial update where some blob data is inconsistent with other data within the container." (XGameSaveSubmitUpdate, Remarks). A title that writes a header blob and a data blob in one update can be left with the new header and the old data after any write error (disk full, a path over MAX_PATH from a long escaped blob name, or a locked file).

Repro (clean-room, Linux):
Copied apply, write_atomic, and enc unchanged into a small standalone test (replace_file takes its std::fs::rename branch; the order of operations is the same on Windows): container with blob a = "old" and display "Old"; an update writing a = "new" and b, where b cannot be written. Result: 0x80004005, blob a = "new", display "New".

Suggested fix:
Stage every write of the update first (each to its own temp file next to its blob), and only after all of them succeed rename them into place and perform the deletes; on any failure, remove the staged temp files and leave the container untouched. A small journal in the container (replayed or discarded on next open) would also close the gap if a rename fails mid-commit. Unit test: an update whose second blob cannot be written leaves the first blob and the display name unchanged.

Not tested: on Windows.

AI use: Initially found and reproduced with Claude Code; re-verified against main (85bdf21) with Antigravity (Gemini 4 Neon).

Activity

  1. oni-swr commented on Nov 24, 2026

    @oni-swr
    Contributor

    I'm taking this one. Plan: apply() first writes every blob (and the display name) to its own temp file next to its destination. Only once all of those writes have succeeded are the files renamed into place and the deletes done. If any write fails, the temp files are removed and the container is left untouched. Unit test in gamesave.rs: an update whose second write fails leaves the first blob and the display name unchanged. A rename failing partway through the commit stays out of scope (no journal).

    AI use: Claude Code (Claude Opus 5.5) helped read the code and draft this comment.

  2. Rednaxeh commented on Nov 24, 2026

    @Rednaxeh
    Contributor

    @oni-swr before you start: I had this one done and tested just before your comment (my mistake not to say so here first). It is on my fork, one commit on current main: Rednaxeh@4012cd9

    Same shape as your plan, plus the part you scoped out: every write goes to a temp file first; then the files the update replaces or deletes are moved aside (under ~xcltmp.* names) and the temps moved into place; a failure anywhere, a rename included, moves the files back and removes the temps. Only the last op on a blob counts, as when the ops ran in order. Test: an update whose second blob cannot be written (a folder in its place) returns E_FAIL and leaves the first blob and the display name as they were; it fails on main (a reads "new"). Whole suite 677 / 0 / 41 on an RX 7800 XT, and a smoke pair on a retail title.

    Not covered, as in yours: a crash between moving the files aside and placing the temps leaves the old blobs under their temp names (a recovery pass on open would be a change of its own).

    Two ways, your call: I open it as a PR and you review or test it, or you take it from here and I test yours. I will not open the PR unless you say so.

  3. oni-swr commented on Nov 24, 2026

    @oni-swr
    Contributor

    @Rednaxeh thanks, and no harm done. Please open it as the PR; I'll review it and close my branch.

    One thing to check while you're at it: the "last op per blob" filter compares names exactly, but on NTFS Meta and META are the same file (see case_insensitive_blob_and_container_matching_on_ntfs). An update that writes Meta and then deletes META seems to leave the blob in place: the delete finds nothing to move aside, since the write's step already moved it, and then the temp is placed. The old in-order code deleted it. I haven't run this, I only read the diff.

  4. Grimkaaah commented on Nov 24, 2026

    @Grimkaaah

    PR opened: stage all writes first, rename only after success; failed submit leaves the container untouched (test included).

    Made with Cursor

  5. added a commit that references this issue on Nov 24, 2026
    d2e70ef
  6. Rednaxeh commented on Nov 24, 2026

    @Rednaxeh
    Contributor

    @oni-swr thanks a lot for the catch, and for reading the diff that closely – you were right: Meta then delete META left the blob. The last-op filter now compares names without case (eq_ignore_ascii_case, as each_container does), and the test has that pair. The PR is #543.

  7. lordon1a commented on Nov 24, 2026

    @lordon1a

    Comparison of the three open PRs for #324 (#491, #543, #621), to help pick one.

    Each one merges cleanly into main f4ce27c and touches only gamesave.rs. I read the diffs before running them and found nothing outside the save folder and the test temp folders. On the merge with main, cargo test -p xcl --lib -- --test-threads=4 gives #491 661 passed, #543 661 passed and #621 662 passed, all with 0 failed.

    The PRs' own tests only cover a failure while staging, so I added two tests that call apply directly. I ran them on each merge and on main:

    // 1. ops on one blob keep their order: delete "a", then write "a" = "z"  ->  "a" must be "z"
    let ops = [("a".to_string(), None), ("a".to_string(), Some(b"z".to_vec()))];
    // 2. a failure while placing (not staging): "c"'s destination is a directory  ->  E_FAIL, "a" still "old"
    let ops = [("a".to_string(), Some(b"new".to_vec())), ("c".to_string(), Some(b"x".to_vec()))];
    delete → write failure while placing PR description check
    main pass fail (#324)
    #491 fail ("a" is deleted) fail ("a" is "new") fail, no smoke blocks
    #543 pass pass pass, 2 smoke blocks
    #621 pass pass fail, no smoke blocks
    • XGameSave: stage all blob writes before renaming (#324) #491 stages all writes, places them, and does all deletes at the end. A delete followed by a write of the same blob in one update now loses the write. That is a regression against main, which runs the ops in order. A failure while placing also keeps the files placed before it, so the update is still half applied whenever the failure comes after staging. Its test injects the failure through a global #[cfg(test)] counter (FAIL_STAGE_AFTER), so it never reaches that path.
    • XGameSave: an update that fails leaves the container as it was (#324) #543 keeps only the last op per blob (without case, as Meta / META are one file on NTFS). It moves the replaced and deleted files aside, places the temps, and restores everything on failure. Both added tests pass.
    • XGameRuntime: a save update applies all its steps or none #621 runs the ops in order, each one moving its old file aside first, and undoes them in reverse on failure. That gets the order right with no last-op filter, and also handles repeated ops on one blob and case-only differences. Both added tests pass. As it stands it cannot be merged (no regression run).

    One point for both #543 and #621: they move the old file aside and then move the new one in. main replaced each blob in a single MoveFileExW(MOVEFILE_REPLACE_EXISTING). Between those two moves, the blob exists only as a ~xcltmp.* file, which blobs_of skips and nothing recovers. A crash or power loss at that moment now loses a blob that main would have kept, old or new. Wrapping that window in ReplaceFileW(dest, tmp, backup) would narrow it, as would recovering leftover backups when a container is opened. That could be a follow-up rather than a reason to hold the fix.

    Suggestion: merge #543, which passes the checks and is correct on both added tests, and close #491 (wrong op order) and #621 as duplicates. #621's in-order approach is the simpler model if the maintainer prefers it, but it needs a regression run first.

    Tested on an i7-12700H with an RTX 3070 Ti Laptop, Windows 11 build 26200. Toolchain: rustc 1.100.0 x86_64-pc-windows-gnullvm, debug, --lib tests only. Not tested: the MSVC toolchain, cargo test --workspace, smoke runs, a real disk-full or locked-file failure.

    Written with Claude Code (Claude Opus 5.5). I checked each point against the diffs and the runs above.

  8. pickles976 commented on Nov 24, 2026

    @pickles976

    Empirical adjudication of the two PRs on this issue (2026-11-24; Linux cross-build + Wine, recipe #520). Shared battery, appended as a unit test to each head — the issue's own scenario: container with blob a and display, one update writing a + a second blob whose path is too long (the "long escaped blob name" from the issue text):

    head update fails? display unchanged? blob a unchanged? battery PR-description check
    main ✅ E_FAIL ❌ "New" ❌ "new" 0/2 (repros the issue) —
    #491 ✅ E_FAIL ❌ "New" — (assert fired first) fails ❌ failure
    #543 ✅ E_FAIL ✅ "Old" ✅ old passes ✅ success (draft)
    #481 / #492 n/a — heads from #326, apply() signature differs, battery doesn't compile n/a ❌ / ✅

    #491's staging is real (the display write is staged) — but the failure this issue describes lands where staging can't see it: with a destination whose final path is over-long, the stage can succeed and the rename into place fails after earlier renames already committed, leaving exactly the half-applied state the issue is about; #543's move-aside-then-rollback survives the same sequence. (Suggest #491 validate/rollback the commit phase, or close as superseded.)

    Recommendation: #543 ready_for_review and merge — it passes the issue's own repro, its description check is already green, and its crash-leftovers cleanup (blobs_of skipping temps) is a useful extra. #491's injection-test idea (test-only failure counter) is worth cherry-picking into it — nice pattern.

    Harness: cargo test -p xcl --lib --target x86_64-pc-windows-gnu zz324rev under Wine 9, per #520; logs kept if useful.

  9. added a commit that references this issue on Nov 24, 2026
    897f5ef
  10. added a commit that references this issue on Nov 24, 2026
    16bcf5b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions