XGameSave: an update that fails leaves the container as it was (#324) #538

Open
Rednaxeh wants to merge 1 commit from upstream-pr/543 into main
Rednaxeh commented 2026-11-24 01:35:50 +00:00 (Migrated from github.com)

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


apply in gamesave.rs wrote an update's display name and then each blob in turn, each replaced atomically on its own. When one write failed partway it returned E_FAIL, but the display name and every blob before the failing one were already replaced, so a title that writes a header blob and a data blob in one update was left with the new header and the old data after any write error (disk full, a path over the limit, a locked file). The GDK documents XGameSaveSubmitUpdate as all or nothing. Fixes #324.

What is in it

  • crates/xcl/src/hle/xgameruntime/gamesave.rs: write_atomic becomes temp_path / write_temp (temp file written and flushed); the move into place is apply's. apply writes every temp first, then moves the files the update replaces or deletes aside and the temps into place; a failure anywhere moves the files back and removes the temps, so the container stays as it was. Only the last op on a blob name counts, as when the ops ran in order (write then delete deletes; delete then write writes), with names compared without case, since Meta and META are one file on NTFS (oni-swr's point on the issue). A restore that fails during the rollback logs the blob and the backup it is left under (oni-swr's review). A new test, a_failed_update_leaves_the_container_as_it_was: an update whose second blob cannot be written (a folder is in its place) returns E_FAIL and leaves the first blob, the display name and the folder free of temp files; then write-then-delete, delete-then-write and write Meta / delete META in one update.

Opened at oni-swr's request after they claimed the issue (their review and branch: see #324). #491 covers the same issue with the staged writes only (no rollback of a rename that fails, and ops are not folded).

Tested

  • AMD Radeon RX 7800 XT (RDNA3), driver 32.0.32029.2010; Ryzen 7 7800X3D, 32 GB; Windows 11 Pro 26300; Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug (release for the regression run); DXC 1.9.2602.17.
  • Without the change (the new test on main's apply): fails at the first check after the failed update, blob a reads "new" instead of "old" (left: [110, 101, 119], right: [111, 108, 100]).
  • With the change: cargo test -p xcl --lib -- hle::xgameruntime::gamesave::: 11 passed, 0 failed (the existing atomic_writes_preserve_xcltmp_blobs_and_skip_leftover_temps now checks the temp cleanup through apply).
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4) with XCL_REQUIRE_GPU=wave64,mesh: 677 passed, 0 failed, 41 ignored, 0 SKIPPED (lib 637 / 0 / 21); no new compiler warning.

Regression run
scripts/smoke.ps1 -Game <folder> on a retail title, release builds, no uncommitted changes, one after the other on the machine above (a warm-up run of main first, not shown). Both blocks unedited.

main 2997c9a:

xcl-smoke v1
commit: 2997c9aca6ebf4577fed34ebdb3cb0a77dd9ec21
gpu: AMD  Radeon RX 7800 XT
title: retail-game6
seconds: 60
exit: 0x0
frames shown: 380 (PresentX 380)
presents per 2 s (last 3): 8 8 8
device removals: 0
crashes: 0
peak commit: 15286 MB

this PR's head 897f5ef:

xcl-smoke v1
commit: 897f5ef85df0d37bdf0dd84b14ccc70558a5d261
gpu: AMD  Radeon RX 7800 XT
title: retail-game6
seconds: 60
exit: 0x0
frames shown: 393 (PresentX 393)
presents per 2 s (last 3): 8 8 8
device removals: 0
crashes: 0
peak commit: 15294 MB

Not tested

  • NVIDIA / Intel. A title's own saves: the regression run gives the title no input in 60 s, so it does not reach a save (it is in its intro movie, whose 8 presents / 2 s on this card are the known cost discussed on #283); it checks that nothing else changed.
  • A crash between moving the files aside and placing the temps leaves the old blobs under their ~xcltmp.* names, which enumeration skips, where before the same crash left a half-applied but visible container. The window is a few renames long; a recovery pass on open would be a change of its own.

AI use

  • Claude Code with Claude Fable 5.1: wrote the change, the test and this description from the issue; the runs are from the machine above.

🤖 Generated with Claude Code

<!-- greyfinch-sync:ivall/xcl:issue:5464404505 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/543). Originally posted by [Rednaxeh](https://github.com/Rednaxeh) on 2026-11-24T01:35:50Z. --- `apply` in `gamesave.rs` wrote an update's display name and then each blob in turn, each replaced atomically on its own. When one write failed partway it returned `E_FAIL`, but the display name and every blob before the failing one were already replaced, so a title that writes a header blob and a data blob in one update was left with the new header and the old data after any write error (disk full, a path over the limit, a locked file). The GDK documents `XGameSaveSubmitUpdate` as all or nothing. Fixes #324. **What is in it** - `crates/xcl/src/hle/xgameruntime/gamesave.rs`: `write_atomic` becomes `temp_path` / `write_temp` (temp file written and flushed); the move into place is `apply`'s. `apply` writes every temp first, then moves the files the update replaces or deletes aside and the temps into place; a failure anywhere moves the files back and removes the temps, so the container stays as it was. Only the last op on a blob name counts, as when the ops ran in order (write then delete deletes; delete then write writes), with names compared without case, since `Meta` and `META` are one file on NTFS (oni-swr's point on the issue). A restore that fails during the rollback logs the blob and the backup it is left under (oni-swr's review). A new test, `a_failed_update_leaves_the_container_as_it_was`: an update whose second blob cannot be written (a folder is in its place) returns `E_FAIL` and leaves the first blob, the display name and the folder free of temp files; then write-then-delete, delete-then-write and write `Meta` / delete `META` in one update. Opened at oni-swr's request after they claimed the issue (their review and branch: see #324). #491 covers the same issue with the staged writes only (no rollback of a rename that fails, and ops are not folded). **Tested** - AMD Radeon RX 7800 XT (RDNA3), driver 32.0.32029.2010; Ryzen 7 7800X3D, 32 GB; Windows 11 Pro 26300; Rust 1.100.0 `stable-x86_64-pc-windows-msvc`, debug (release for the regression run); DXC 1.9.2602.17. - Without the change (the new test on `main`'s `apply`): fails at the first check after the failed update, blob `a` reads `"new"` instead of `"old"` (`left: [110, 101, 119], right: [111, 108, 100]`). - With the change: `cargo test -p xcl --lib -- hle::xgameruntime::gamesave::`: 11 passed, 0 failed (the existing `atomic_writes_preserve_xcltmp_blobs_and_skip_leftover_temps` now checks the temp cleanup through `apply`). - `.\scripts\test.ps1` (`cargo build --workspace` + `cargo test --workspace -- --test-threads=4`) with `XCL_REQUIRE_GPU=wave64,mesh`: **677 passed, 0 failed, 41 ignored, 0 SKIPPED** (lib 637 / 0 / 21); no new compiler warning. **Regression run** `scripts/smoke.ps1 -Game <folder>` on a retail title, release builds, no uncommitted changes, one after the other on the machine above (a warm-up run of `main` first, not shown). Both blocks unedited. `main` `2997c9a`: ``` xcl-smoke v1 commit: 2997c9aca6ebf4577fed34ebdb3cb0a77dd9ec21 gpu: AMD Radeon RX 7800 XT title: retail-game6 seconds: 60 exit: 0x0 frames shown: 380 (PresentX 380) presents per 2 s (last 3): 8 8 8 device removals: 0 crashes: 0 peak commit: 15286 MB ``` this PR's head `897f5ef`: ``` xcl-smoke v1 commit: 897f5ef85df0d37bdf0dd84b14ccc70558a5d261 gpu: AMD Radeon RX 7800 XT title: retail-game6 seconds: 60 exit: 0x0 frames shown: 393 (PresentX 393) presents per 2 s (last 3): 8 8 8 device removals: 0 crashes: 0 peak commit: 15294 MB ``` **Not tested** - NVIDIA / Intel. A title's own saves: the regression run gives the title no input in 60 s, so it does not reach a save (it is in its intro movie, whose 8 presents / 2 s on this card are the known cost discussed on #283); it checks that nothing else changed. - A crash between moving the files aside and placing the temps leaves the old blobs under their `~xcltmp.*` names, which enumeration skips, where before the same crash left a half-applied but visible container. The window is a few renames long; a recovery pass on open would be a change of its own. **AI use** - Claude Code with Claude Fable 5.1: wrote the change, the test and this description from the issue; the runs are from the machine above. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
oni-swr commented 2026-11-24 10:04:05 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by oni-swr on 2026-11-24T10:04:05Z.

Review: COMMENTED


Review of #543 at d2e70ef, as promised on #324. I read the whole diff.

The logic is right as far as I can see. On a failure, the temps already moved into place are undone in the order they were placed, then the files moved aside go back. A failure while moving files aside only restores them. Comparing names case-insensitively for ASCII only is enough: enc escapes every non-ASCII byte, so names that differ in non-ASCII case are different files anyway.

Tested: merged onto main 9aec53d (clean), cargo test -p xcl --lib -- hle::xgameruntime::gamesave::: 11 passed, 0 failed. RX 7800 XT, Ryzen 7 5700X3D, Windows 11 Pro 26200, Rust 1.100.0 MSVC, debug. Not run: the whole suite or a smoke pair (yours stand).

Two points, neither blocking:

  1. The crash window lordon1a described. Every blob is moved aside before any temp is placed, so the window covers the whole update. During it, a blob exists only under a ~xcltmp.* name, which blobs_of skips. A title crashing on another thread can end the process mid-save, so this isn't only a power-loss case. One fix per blob: hard-link the old file to the backup name (copy if links fail), then MoveFileExW(temp, dest, REPLACE_EXISTING) as main does. Then dest always holds the old or the new contents, and rollback is still replace_file(backup, dest). My closed branch does this and passes the 11 gamesave tests. I can send it as a follow-up after this merges, or you take it, your call.
  2. A failed restore is silent. If replace_file(away, dest) fails during rollback, the old blob stays under a temp name and the title sees it as deleted, with nothing in the log. A log line there would make it findable.

Once it's out of draft I'd merge it as is; #491 then duplicates it. Closing my branch.

AI use: Claude Code (Claude Opus 5.5) read the diff and ran the tests; I checked each point.

<!-- greyfinch-sync:ivall/xcl:review:6037204083 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/543#pullrequestreview-6037204083). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-24T10:04:05Z. **Review:** COMMENTED --- Review of #543 at `d2e70ef`, as promised on #324. I read the whole diff. The logic is right as far as I can see. On a failure, the temps already moved into place are undone in the order they were placed, then the files moved aside go back. A failure while moving files aside only restores them. Comparing names case-insensitively for ASCII only is enough: `enc` escapes every non-ASCII byte, so names that differ in non-ASCII case are different files anyway. Tested: merged onto `main` 9aec53d (clean), `cargo test -p xcl --lib -- hle::xgameruntime::gamesave::`: 11 passed, 0 failed. RX 7800 XT, Ryzen 7 5700X3D, Windows 11 Pro 26200, Rust 1.100.0 MSVC, debug. Not run: the whole suite or a smoke pair (yours stand). Two points, neither blocking: 1. **The crash window lordon1a described.** Every blob is moved aside before any temp is placed, so the window covers the whole update. During it, a blob exists only under a `~xcltmp.*` name, which `blobs_of` skips. A title crashing on another thread can end the process mid-save, so this isn't only a power-loss case. One fix per blob: hard-link the old file to the backup name (copy if links fail), then `MoveFileExW(temp, dest, REPLACE_EXISTING)` as `main` does. Then `dest` always holds the old or the new contents, and rollback is still `replace_file(backup, dest)`. My closed branch does this and passes the 11 gamesave tests. I can send it as a follow-up after this merges, or you take it, your call. 2. **A failed restore is silent.** If `replace_file(away, dest)` fails during rollback, the old blob stays under a temp name and the title sees it as deleted, with nothing in the log. A log line there would make it findable. Once it's out of draft I'd merge it as is; #491 then duplicates it. Closing my branch. AI use: Claude Code (Claude Opus 5.5) read the diff and ran the tests; I checked each point.
Rednaxeh commented 2026-11-24 11:05:42 +00:00 (Migrated from github.com)

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


@oni-swr thank you for the review and the run. Taken as follows:

  1. A failed restore now logs a warning with the backup's path and the blob it belongs to (d2e70ef → new head), so a blob that "vanished" after a failed save can be found and put back by hand. Smoke pair rerun on the new head, in the description.
  2. Agreed that the window should go, and the hard link is the right tool: dest then always holds the old or the new contents. Please send it as the follow-up once this is in – your branch has it and the tests; I'd rather not fold it into this PR after the reviews it has had.

Marked ready for review. AI use: Claude Code (Claude Fable 5.1) wrote the change and this reply; the run was mine.

<!-- greyfinch-sync:ivall/xcl:comment:7128598965 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/543#issuecomment-7128598965). Originally posted by [Rednaxeh](https://github.com/Rednaxeh) on 2026-11-24T10:18:30Z. --- @oni-swr thank you for the review and the run. Taken as follows: 2. A failed restore now logs a warning with the backup's path and the blob it belongs to (`d2e70ef` → new head), so a blob that "vanished" after a failed save can be found and put back by hand. Smoke pair rerun on the new head, in the description. 1. Agreed that the window should go, and the hard link is the right tool: `dest` then always holds the old or the new contents. Please send it as the follow-up once this is in – your branch has it and the tests; I'd rather not fold it into this PR after the reviews it has had. Marked ready for review. AI use: Claude Code (Claude Fable 5.1) wrote the change and this reply; the run was mine.
Belox17 commented 2026-11-24 19:07:37 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Belox17 on 2026-11-24T18:48:51Z.


Test report for 897f5ef8 on Windows (RTX 3060 Ti): passes on its base and merged into current main; the test fails without the fix

Tested

  • Ryzen 5 5600X, NVIDIA GeForce RTX 3060 Ti; Windows 11 build 26300 (NTFS); rustc 1.98.1 MSVC; debug build. PR head as is, clean tree.
  • cargo test -p xcl --lib a_failed_update_leaves_the_container: 1 passed.
  • cargo test --workspace -- --test-threads=4 on the head: 677 passed, 0 failed, 41 ignored (its base 2997c9ac: 676).
  • Merged into current main 9e3831e (clean merge): 777 passed, 0 failed, 41 ignored (main alone: 776).
  • With gamesave.rs from 2997c9ac plus only the new test, the test fails: blob a is new (left [110, 101, 119]) instead of old after the failed update.
  • The same test, run against #1531's gamesave.rs (same issue), fails at the Meta / META case: there, an update that writes Meta and then deletes META keeps the blob, as GonZiin's review on #1531 found. This PR handles it.

Not tested

  • The crash window described in the commit message; a title's save path; AMD.

AI use: Claude Code (Claude Opus 5.5) ran the commands and wrote this report; I checked it before posting.

<!-- greyfinch-sync:ivall/xcl:comment:7141188291 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/543#issuecomment-7141188291). Originally posted by [Belox17](https://github.com/Belox17) on 2026-11-24T18:48:51Z. --- **Test report for `897f5ef8` on Windows (RTX 3060 Ti): passes on its base and merged into current main; the test fails without the fix** **Tested** - Ryzen 5 5600X, NVIDIA GeForce RTX 3060 Ti; Windows 11 build 26300 (NTFS); rustc 1.98.1 MSVC; debug build. PR head as is, clean tree. - `cargo test -p xcl --lib a_failed_update_leaves_the_container`: 1 passed. - `cargo test --workspace -- --test-threads=4` on the head: 677 passed, 0 failed, 41 ignored (its base `2997c9ac`: 676). - Merged into current `main` `9e3831e` (clean merge): **777 passed, 0 failed, 41 ignored** (`main` alone: 776). - With `gamesave.rs` from `2997c9ac` plus only the new test, the test fails: blob `a` is `new` (left `[110, 101, 119]`) instead of `old` after the failed update. - The same test, run against #1531's `gamesave.rs` (same issue), fails at the `Meta` / `META` case: there, an update that writes `Meta` and then deletes `META` keeps the blob, as GonZiin's review on #1531 found. This PR handles it. **Not tested** - The crash window described in the commit message; a title's save path; AMD. **AI use:** Claude Code (Claude Opus 5.5) ran the commands and wrote this report; I checked it before posting.
luckyteagirl commented 2026-11-25 04:10:22 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by luckyteagirl on 2026-11-25T03:15:21Z.


Tested

  • Exact PR head 897f5ef85df0d37bdf0dd84b14ccc70558a5d261 on Windows 11 build 26200, Ryzen 9 5980HX, Rust 1.99.0 MSVC. This is a filesystem and GameSave test; it did not exercise the RX 6800M GPU.
  • cargo test --locked -p xcl --lib -j 2 hle::xgameruntime::gamesave:: -- --test-threads=1 --show-output: 11 passed, 0 failed, 0 ignored, no explicit skips. a_failed_update_leaves_the_container_as_it_was passed, including preservation of the old blob and display name when the second write fails, temp cleanup, and last-operation behavior for case variants of a blob name.
  • cargo build --locked --workspace -j 2: passed.

Not tested

  • A title's save or load flow, the full workspace suite, other filesystems or operating systems, or interruption between moving old files aside and putting new files in place. The last case is the crash window already described in the PR.

AI use

OpenAI Codex (GPT-6, exact variant not exposed in this session) reviewed the exact diff, ran the GameSave suite and build, checked the logs, and drafted this report.

<!-- greyfinch-sync:ivall/xcl:comment:7151740302 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/543#issuecomment-7151740302). Originally posted by [luckyteagirl](https://github.com/luckyteagirl) on 2026-11-25T03:15:21Z. --- **Tested** - Exact PR head `897f5ef85df0d37bdf0dd84b14ccc70558a5d261` on Windows 11 build 26200, Ryzen 9 5980HX, Rust 1.99.0 MSVC. This is a filesystem and GameSave test; it did not exercise the RX 6800M GPU. - `cargo test --locked -p xcl --lib -j 2 hle::xgameruntime::gamesave:: -- --test-threads=1 --show-output`: **11 passed, 0 failed, 0 ignored**, no explicit skips. `a_failed_update_leaves_the_container_as_it_was` passed, including preservation of the old blob and display name when the second write fails, temp cleanup, and last-operation behavior for case variants of a blob name. - `cargo build --locked --workspace -j 2`: passed. **Not tested** - A title's save or load flow, the full workspace suite, other filesystems or operating systems, or interruption between moving old files aside and putting new files in place. The last case is the crash window already described in the PR. **AI use** OpenAI Codex (GPT-6, exact variant not exposed in this session) reviewed the exact diff, ran the GameSave suite and build, checked the logs, and drafted this report.
DavidM1911 commented 2026-11-25 14:10:05 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-25T13:43:15Z.


Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.)

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7164190506 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/543#issuecomment-7164190506). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-25T13:43:15Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.) Thanks!
gut0to commented 2026-11-26 16:43:13 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-11-26T16:43:13Z.

Review: COMMENTED


AI-assisted review (OpenAI GPT-6); I inspected the current diff and tests. I did not run tests or the game.

<!-- greyfinch-sync:ivall/xcl:review:6077732940 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/543#pullrequestreview-6077732940). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-26T16:43:13Z. **Review:** COMMENTED --- AI-assisted review (OpenAI GPT-6); I inspected the current diff and tests. I did not run tests or the game.
gut0to commented 2026-11-26 16:43:13 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-11-26T16:43:13Z.

Code review location: crates/xcl/src/hle/xgameruntime/gamesave.rs; line 619

@@ -567,24 +571,60 @@ fn apply(dir: &Path, display: Option<&str>, ops: &[(String, Option<Vec<u8>>)]) -
     if std::fs::create_dir_all(&blobs).is_err() {
         return crate::util::E_FAIL;
     }
-    if let Some(d) = display {
-        if write_atomic(&dir.join("display"), d.as_bytes()).is_err() {
-            return crate::util::E_FAIL;
+    // All or nothing (#324): every write goes to a temp file first; then the files the update
+    // replaces or deletes are moved aside and the temps moved into place. A failure anywhere
+    // puts the files moved aside back and removes the temps, so the container stays as it was.
+    let mut writes: Vec<(PathBuf, PathBuf)> = Vec::new(); // (temp, destination)
+    let mut deletes: Vec<PathBuf> = Vec::new();
+    let mut aside: Vec<(PathBuf, PathBuf)> = Vec::new(); // (where it went, where it was)
+    let mut placed = 0;
+    let done = (|| -> std::io::Result<()> {
+        if let Some(d) = display {
+            let dest = dir.join("display");
+            writes.push((write_temp(&dest, d.as_bytes())?, dest));
         }
-    }
-    for (name, data) in ops {
-        let path = blobs.join(enc(name));
-        match data {
-            Some(d) => {
-                // write to a unique sibling temp file, sync, then replace atomically
-                if write_atomic(&path, d).is_err() {
-                    return crate::util::E_FAIL;
-                }
+        // the last op on a blob is the one that counts, as when they ran in order (names
+        // compare without case: one file on NTFS)
+        let mut seen: Vec<&str> = Vec::new();
+        let last: Vec<&(String, Option<Vec<u8>>)> =
+            ops.iter().rev().filter(|(name, _)| !seen.iter().any(|s| s.eq_ignore_ascii_case(name)) && { seen.push(name); true }).collect();
+        for (name, data) in last.into_iter().rev() {
+            let path = blobs.join(enc(name));
+            match data {
+                Some(d) => writes.push((write_temp(&path, d)?, path)),
+                None => deletes.push(path),
+            }
+        }
+        for dest in writes.iter().map(|(_, dest)| dest).chain(&deletes) {
+            if dest.is_file() {
+                let away = temp_path(dest)?;
+                replace_file(dest, &away)?;
+                aside.push((away, dest.clone()));
+            }
+        }
+        for (tmp, dest) in &writes {
+            replace_file(tmp, dest)?;
+            placed += 1;
+        }
+        Ok(())
+    })();
+    if done.is_err() {
+        for (tmp, dest) in &writes {
+            let _ = std::fs::remove_file(tmp);
+            if placed > 0 {
+                let _ = std::fs::remove_file(dest);
+                placed -= 1;
             }
-            None => {
-                let _ = std::fs::remove_file(&path);
+        }
+        for (away, dest) in &aside {

Could the regression also cover rollback of a delete when a later write fails? This branch restores every file moved aside, including deleted blobs, but the current failed-update test only verifies restoration of a replaced blob and the display name. A focused update that deletes an existing blob and then fails while installing a later write would assert the deleted blob is restored and the container remains unchanged. AI-assisted review (OpenAI GPT-6): I inspected the diff and tests; I did not run tests.

<!-- greyfinch-sync:ivall/xcl:review-comment:4711257332 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/543#discussion_r4711257332). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-26T16:43:13Z. **Code review location:** `crates/xcl/src/hle/xgameruntime/gamesave.rs`; line 619 ~~~~diff @@ -567,24 +571,60 @@ fn apply(dir: &Path, display: Option<&str>, ops: &[(String, Option<Vec<u8>>)]) - if std::fs::create_dir_all(&blobs).is_err() { return crate::util::E_FAIL; } - if let Some(d) = display { - if write_atomic(&dir.join("display"), d.as_bytes()).is_err() { - return crate::util::E_FAIL; + // All or nothing (#324): every write goes to a temp file first; then the files the update + // replaces or deletes are moved aside and the temps moved into place. A failure anywhere + // puts the files moved aside back and removes the temps, so the container stays as it was. + let mut writes: Vec<(PathBuf, PathBuf)> = Vec::new(); // (temp, destination) + let mut deletes: Vec<PathBuf> = Vec::new(); + let mut aside: Vec<(PathBuf, PathBuf)> = Vec::new(); // (where it went, where it was) + let mut placed = 0; + let done = (|| -> std::io::Result<()> { + if let Some(d) = display { + let dest = dir.join("display"); + writes.push((write_temp(&dest, d.as_bytes())?, dest)); } - } - for (name, data) in ops { - let path = blobs.join(enc(name)); - match data { - Some(d) => { - // write to a unique sibling temp file, sync, then replace atomically - if write_atomic(&path, d).is_err() { - return crate::util::E_FAIL; - } + // the last op on a blob is the one that counts, as when they ran in order (names + // compare without case: one file on NTFS) + let mut seen: Vec<&str> = Vec::new(); + let last: Vec<&(String, Option<Vec<u8>>)> = + ops.iter().rev().filter(|(name, _)| !seen.iter().any(|s| s.eq_ignore_ascii_case(name)) && { seen.push(name); true }).collect(); + for (name, data) in last.into_iter().rev() { + let path = blobs.join(enc(name)); + match data { + Some(d) => writes.push((write_temp(&path, d)?, path)), + None => deletes.push(path), + } + } + for dest in writes.iter().map(|(_, dest)| dest).chain(&deletes) { + if dest.is_file() { + let away = temp_path(dest)?; + replace_file(dest, &away)?; + aside.push((away, dest.clone())); + } + } + for (tmp, dest) in &writes { + replace_file(tmp, dest)?; + placed += 1; + } + Ok(()) + })(); + if done.is_err() { + for (tmp, dest) in &writes { + let _ = std::fs::remove_file(tmp); + if placed > 0 { + let _ = std::fs::remove_file(dest); + placed -= 1; } - None => { - let _ = std::fs::remove_file(&path); + } + for (away, dest) in &aside { ~~~~ --- Could the regression also cover rollback of a delete when a later write fails? This branch restores every file moved aside, including deleted blobs, but the current failed-update test only verifies restoration of a replaced blob and the display name. A focused update that deletes an existing blob and then fails while installing a later write would assert the deleted blob is restored and the container remains unchanged. AI-assisted review (OpenAI GPT-6): I inspected the diff and tests; I did not run tests.
github-actions[bot] commented 2026-12-01 02:35:23 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-12-01T02:35:23Z.


Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

<!-- greyfinch-sync:ivall/xcl:comment:7298778630 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/543#issuecomment-7298778630). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-12-01T02:35:23Z. --- Nothing has happened on this PR for 3 days, so it is marked `stale` and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto `main` if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.
Foniass commented 2026-12-01 03:41:07 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T03:41:07Z.


Triage (#1607) – head 897f5ef

#1531 (same staged write and rollback for #324) and #491 (staging only) are closed; no other open PR changes apply in gamesave.rs (#2132 and #2521 touch reads and blob sizes). Main still replaces each blob on its own in write_atomic, so the issue is not fixed there. Read the rollback: placed temps are removed in placement order, files moved aside are put back, and the test fails on main's apply. No problems found in the diff.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7299958941 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/543#issuecomment-7299958941). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T03:41:07Z. --- **Triage (#1607)** – head 897f5ef #1531 (same staged write and rollback for #324) and #491 (staging only) are closed; no other open PR changes `apply` in gamesave.rs (#2132 and #2521 touch reads and blob sizes). Main still replaces each blob on its own in `write_atomic`, so the issue is not fixed there. Read the rollback: placed temps are removed in placement order, files moved aside are put back, and the test fails on main's `apply`. No problems found in the diff. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
fromthemoonx commented 2026-12-02 20:33:11 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by fromthemoonx on 2026-12-02T20:33:11Z.


merge-check
base: 5ad98adbc3
head: 897f5ef85d
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 3006 passed, 0 failed, 52 ignored (3004 / 0 / 52 on 817eb1f, plus this PR's test and 1 that main gained since); SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps); failing: none
review: read the whole diff (1 file, +124 -25). The rollback after a failed write reads right and is tested, and main still has #324.

  1. Blocking, because these are save files: if the process dies during an update, blobs can be gone from the container. The old files are moved aside before the new ones take their names. In between, the container has neither, and the old data sits in ~xcltmp.* files that XCL skips and never puts back. The description mentions this window and calls it a few renames long. I measured it: a process saves three 4 KB blobs in a loop, gets killed at a varying moment, and the container is read afterwards. 400 kills per run, two runs each on NTFS:
after the kill main this PR this PR with the change below
all blobs from one save 139, 129 287, 291 339, 341
old and new blobs mixed 261, 271 0, 0 61, 59
blobs missing 0, 0 113, 109 0, 0

The process does nothing but save, so these are rates for a kill during a save, not for crashes in general. Main never loses a blob this way.
2) A small change closes it. The old blob gets a second name with a hard link (a copy where the volume has none) and keeps its own until the new one replaces it. That is the hard link oni-swr suggested, which was left for a follow-up. I think it belongs in this PR, since without it the PR adds the missing-blob case.

diff --git a/crates/xcl/src/hle/xgameruntime/gamesave.rs b/crates/xcl/src/hle/xgameruntime/gamesave.rs
index 11ae97ab..4ac16cf8 100644
--- a/crates/xcl/src/hle/xgameruntime/gamesave.rs
+++ b/crates/xcl/src/hle/xgameruntime/gamesave.rs
@@ -616,7 +616,18 @@ fn apply(dir: &Path, display: Option<&str>, ops: &[(String, Option<Vec<u8>>)]) -
                 None => deletes.push(path),
             }
         }
-        for dest in writes.iter().map(|(_, dest)| dest).chain(&deletes) {
+        // (a blob to replace keeps its name until the new one takes it: a process that dies
+        // here leaves the old or the new blob, never none)
+        for (_, dest) in &writes {
+            if dest.is_file() {
+                let away = temp_path(dest)?;
+                if std::fs::hard_link(dest, &away).is_err() {
+                    std::fs::copy(dest, &away)?; // no hard links on this volume
+                }
+                aside.push((away, dest.clone()));
+            }
+        }
+        for dest in &deletes {
             if dest.is_file() {
                 let away = temp_path(dest)?;
                 replace_file(dest, &away)?;

With it the gamesave tests pass, this PR's included. So does a scratch test where the first blob cannot be placed: the second keeps its old content, a blob to delete is still there, and no temp files are left. I did not run the whole suite with this change. Deletes still move the file aside first, so a kill there can take effect early for a blob the update was deleting anyway.
3) Two of the three results are better than main either way: no mixed saves in the PR as it is, and far fewer with the change. A recovery step when a container is opened would remove the rest, as its own PR.
4) This head shows only the PR description check. Build and test (Windows, no GPU) has not run on it.
5) Not tested: a retail title (none on this machine), a power cut, and volumes without hard links.
performance: one link and one delete more per replaced blob (reasoned, not measured)
verdict: not yet: a process that dies while saving can leave a container without some of its blobs, which main never does; the change above fixes it here
hardware / tool and model: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300 / Claude Code (Claude Opus 5.5)

<!-- greyfinch-sync:ivall/xcl:comment:7357921060 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/543#issuecomment-7357921060). Originally posted by [fromthemoonx](https://github.com/fromthemoonx) on 2026-12-02T20:33:11Z. --- merge-check base: 5ad98adbc360225ac8a57ad9661e16fe8df9d8dd head: 897f5ef85df0d37bdf0dd84b14ccc70558a5d261 merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 3006 passed, 0 failed, 52 ignored (3004 / 0 / 52 on 817eb1f, plus this PR's test and 1 that main gained since); SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps); failing: none review: read the whole diff (1 file, +124 -25). The rollback after a failed write reads right and is tested, and main still has #324. 1) Blocking, because these are save files: if the process dies during an update, blobs can be gone from the container. The old files are moved aside before the new ones take their names. In between, the container has neither, and the old data sits in `~xcltmp.*` files that XCL skips and never puts back. The description mentions this window and calls it a few renames long. I measured it: a process saves three 4 KB blobs in a loop, gets killed at a varying moment, and the container is read afterwards. 400 kills per run, two runs each on NTFS: | after the kill | main | this PR | this PR with the change below | |---|---|---|---| | all blobs from one save | 139, 129 | 287, 291 | 339, 341 | | old and new blobs mixed | 261, 271 | 0, 0 | 61, 59 | | blobs missing | 0, 0 | **113, 109** | 0, 0 | The process does nothing but save, so these are rates for a kill during a save, not for crashes in general. Main never loses a blob this way. 2) A small change closes it. The old blob gets a second name with a hard link (a copy where the volume has none) and keeps its own until the new one replaces it. That is the hard link oni-swr suggested, which was left for a follow-up. I think it belongs in this PR, since without it the PR adds the missing-blob case. ```diff diff --git a/crates/xcl/src/hle/xgameruntime/gamesave.rs b/crates/xcl/src/hle/xgameruntime/gamesave.rs index 11ae97ab..4ac16cf8 100644 --- a/crates/xcl/src/hle/xgameruntime/gamesave.rs +++ b/crates/xcl/src/hle/xgameruntime/gamesave.rs @@ -616,7 +616,18 @@ fn apply(dir: &Path, display: Option<&str>, ops: &[(String, Option<Vec<u8>>)]) - None => deletes.push(path), } } - for dest in writes.iter().map(|(_, dest)| dest).chain(&deletes) { + // (a blob to replace keeps its name until the new one takes it: a process that dies + // here leaves the old or the new blob, never none) + for (_, dest) in &writes { + if dest.is_file() { + let away = temp_path(dest)?; + if std::fs::hard_link(dest, &away).is_err() { + std::fs::copy(dest, &away)?; // no hard links on this volume + } + aside.push((away, dest.clone())); + } + } + for dest in &deletes { if dest.is_file() { let away = temp_path(dest)?; replace_file(dest, &away)?; ``` With it the `gamesave` tests pass, this PR's included. So does a scratch test where the first blob cannot be placed: the second keeps its old content, a blob to delete is still there, and no temp files are left. I did not run the whole suite with this change. Deletes still move the file aside first, so a kill there can take effect early for a blob the update was deleting anyway. 3) Two of the three results are better than main either way: no mixed saves in the PR as it is, and far fewer with the change. A recovery step when a container is opened would remove the rest, as its own PR. 4) This head shows only the `PR description` check. `Build and test (Windows, no GPU)` has not run on it. 5) Not tested: a retail title (none on this machine), a power cut, and volumes without hard links. performance: one link and one delete more per replaced blob (reasoned, not measured) verdict: not yet: a process that dies while saving can leave a container without some of its blobs, which main never does; the change above fixes it here hardware / tool and model: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300 / Claude Code (Claude Opus 5.5)
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/543:upstream-pr/543'
git switch 'upstream-pr/543'

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