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

Open
opened 2026-11-23 23:27:48 +00:00 by mastrr9 · 7 comments
mastrr9 commented 2026-11-23 23:27:48 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by mastrr9 on 2026-11-23T23:27:48Z.


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

<!-- greyfinch-sync:ivall/xcl:issue:6570991544 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/324). Originally posted by [mastrr9](https://github.com/mastrr9) on 2026-11-23T23:27:48Z. --- `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).
oni-swr commented 2026-11-24 07:05:03 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by oni-swr on 2026-11-24T00:43:54Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7118372543 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/324#issuecomment-7118372543). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-24T00:43:54Z. --- 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.
Rednaxeh commented 2026-11-24 07:05:05 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by Rednaxeh on 2026-11-24T00:49:18Z.


@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: github.com/Rednaxeh/xcl@4012cd9258

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.

<!-- greyfinch-sync:ivall/xcl:comment:7118467497 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/324#issuecomment-7118467497). Originally posted by [Rednaxeh](https://github.com/Rednaxeh) on 2026-11-24T00:49:18Z. --- @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`: https://github.com/Rednaxeh/xcl/commit/4012cd925853ec4dffe2e3cfefc01320b725b6b8 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.
oni-swr commented 2026-11-24 07:05:07 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by oni-swr on 2026-11-24T00:57:24Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7118607625 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/324#issuecomment-7118607625). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-24T00:57:24Z. --- @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.
Grimkaaah commented 2026-11-24 07:05:07 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by Grimkaaah on 2026-11-24T01:00:04Z.


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

Made with Cursor

<!-- greyfinch-sync:ivall/xcl:comment:7118651523 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/324#issuecomment-7118651523). Originally posted by [Grimkaaah](https://github.com/Grimkaaah) on 2026-11-24T01:00:04Z. --- PR opened: stage all writes first, rename only after success; failed submit leaves the container untouched (test included). Made with [Cursor](https://cursor.com)
Rednaxeh commented 2026-11-24 07:05:19 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by Rednaxeh on 2026-11-24T01:35:52Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7119259044 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/324#issuecomment-7119259044). Originally posted by [Rednaxeh](https://github.com/Rednaxeh) on 2026-11-24T01:35:52Z. --- @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.
lordon1a commented 2026-11-24 07:06:03 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by lordon1a on 2026-11-24T03:57:53Z.


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
  • #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.
  • #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.
  • #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.

<!-- greyfinch-sync:ivall/xcl:comment:7121581903 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/324#issuecomment-7121581903). Originally posted by [lordon1a](https://github.com/lordon1a) on 2026-11-24T03:57:53Z. --- 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`: ```rust // 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 | - **#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. - **#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. - **#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.
pickles976 commented 2026-11-24 07:06:26 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by pickles976 on 2026-11-24T05:01:05Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7122546387 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/324#issuecomment-7122546387). Originally posted by [pickles976](https://github.com/pickles976) on 2026-11-24T05:01:05Z. --- 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.
Sign in to join this conversation.
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
xcl-dev/xcl#321
No description provided.