fix(saves): longshot_saves.py puts the copies on the disk before the swap #3013

Open
PedroRossi wants to merge 2 commits from PedroRossi/Longshot:fix/save-tool-flush into main
Contributor

Summary

A power loss right after the save tool says Done can lose the save it was protecting. copy_saves
(scripts/longshot_saves.py) copies the save, backs the target up, then swaps the copy in with renames, and nothing is
flushed: nothing orders the copies' data before those renames. On ext4 (Linux, and the Steam Deck's drive and SD
cards) a rename can reach the disk well before the data of the files it moves, so a power cut or a hang in that window
can leave empty or cut files in place of the slots, and the backup the same, with the old save already moved away.

on_disk, called right before swap_in, puts the copies on the disk first: os.sync() where Python has it (one call,
every file), else each copied file's fsync (Windows).

How to see it on main: nothing shows without a power cut at that moment; the test checks the order (the copies synced
before the swap).

Read first: on_disk and its call in copy_saves.

Testing

Tested: on this PC (Debian 13, ext4 on NVMe): by hand, the real tool exported a save from a game run over
an emulator's (export --longshot DIR --to DIR --backup-dir DIR), each copy tagged in its last bytes so the two
differ: the emulator's folder then held Longshot's save, the backup the emulator's old one, Done. The new test fails on main and passes here, with the rest of test_longshot_saves.py; ruff.
mise run test (--strict) on main @ 1938a302, and on main with this change merged (together with #3009, #3010, #3011, #3012, #3014, from the same audit): 775 passed, 145 failed, 10 skipped both ways, so none fails only with it. The 145 are named at the end.

Still to test:

  • Windows, by hand: export a save, expecting the same (there each copied file is fsynced one by
    one: that branch didn't run here)

Measurements

Verdict: an import or export takes about 8 ms more on this PC (the median), once per copy.

Setup: Debian 13, ext4 on NVMe, through the PC's job queue (timing-cpu: nothing else running); the whole export
command for a real save (account.sav, option.sav, one slot) over another, with a backup; main's script and this one
alternating, 9 runs each.

Before: main @ 1938a302 After: this PR @ e8cfa476
export, median (ms) 46 54
export, fastest to slowest (ms) 44 to 48 49 to 71

Risks

os.sync() flushes all of the system's pending writes, not only the copies': with much pending (a big copy just
before, a slow SD card) Done comes a little later. The tool is run by hand, once per copy. A revert is enough.

Checklist

Ready for review

  • One topic, on current main; the title is a Conventional Commit.
  • 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).
  • 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: a game-free test that fails on main and passes here.
  • A check under tests/ that reads changed source is updated, not weakened.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

Ready to merge

  • Everything in Still to test has run, its results in Tested.
  • Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers").
  • Approved in review, every comment answered.
  • CI passes on the head; on current main, measured again or a comment says why the numbers still hold.
The 145 checks of mise run test that fail on main @ 1938a302 on this PC too, with or without this change

97 need a tool this PC doesn't have on PATH (--strict fails them):

  • clang (63): test_audio_output_devices.py, test_box_light_reuse.py, test_camera_centering.py, test_camera_speed.py, test_drive_target.py, test_dynres_sites.py, test_exclusive_marks.py, test_fixed_scale_sites.py, test_floor_carry_step.py, test_frame_rate_cap_step.py, test_frame_whole.py, test_free_view_anchor.py, test_fullscreen_monitor_platform.py, test_high_fps_fixes.py, test_horse_jump_step.py, test_hos_ab.py, test_hud_layout.py, test_jump_takeoff_step.py, test_linux_audio_output.py, test_loop_step.py, test_menu_hooks.py, test_microphone_test_gain.py, test_monitor_friendly_names.py, test_mouse_native.py, test_movement_speed.py, test_movie_frame_count.py, test_nvn_null_pool.py, test_nvn_tex_builder_guard.py, test_ocarina_wait.py, test_ocb_hold.py, test_oot_barrier.py, test_oot_camera_clearance.py, test_oot_cloth.py, test_oot_cloth_60.py, test_oot_cloth_time.py, test_oot_damage.py, test_oot_fov.py, test_oot_heap_retry.py, test_oot_jitter.py, test_oot_shadow.py, test_oot_shadow_fit.py, test_oot_transition_autosave.py, test_oot_voice_option.py, test_oot_wide_bubble.py, test_oot_wide_save_picture.py, test_player_update_sites.py, test_sampler_stack_walk.py, test_saved_turn_rate.py, test_scarecrow_timing.py, test_scene_format.py, test_sdl_monitor_names.py, test_shadow_slot.py, test_song_note_cue.py, test_sound_held_frames.py, test_sprint.py, test_svc_core_mask.py, test_tex_compact.py, test_text_speed.py, test_title_card_sites.py, test_water_substeps.py, test_web_floor_steps.py, test_wide_sites.py, test_wide_ui.py
  • spirv-val (20): f2i_immediate, global_load_16, native_suatom, test_coop_spirv.py, test_default_point_size.py, test_depth_sample_types.py, test_draw_parameters.py, test_fsr4_tune.py, test_guest_page_fast32.py, test_image_arrays.py, test_mesh_fallback_layout.py, test_mesh_unreachable_outputs.py, test_mma_software.py, test_native_float_atomic_add.py, test_nonuniform_image_arrays.py, test_spirv_struct_identity.py, test_texture_phi_dominance.py, test_unsigned_image_types.py, test_vendor_translation.py, test_wmma16_spirv_val.py
  • clang++ (7): test_dead_targets_sweep.py, test_lower_depbar.py, test_lower_lds_after_sts.py, test_lower_register_markers.py, test_native_suatom.py, test_program_answer_domains.py, test_target_pool_cap.py
  • glslang (5): test_framegen_hdr.py, test_fsr3fi_hud_layer.py, test_fsr3fi_hudless_colour.py, test_host_shaders.py, test_temporal_present_bandwidth.py
  • glslang, spirv-val (1): test_fsr3_shaders.py
  • llvm-mc (1): test_lifter.py

48 fail on main itself: test_blocks_at_start.py, test_bug_report.py, test_cloth_kernels.py, test_cloth_loops.py, test_cloth_sphere.py, test_cloth_step12.py, test_cloth_step8.py, test_cloth_step9.py, test_control_picture.py, test_crash_record.py, test_crt_options.py, test_dev_update.py, test_dlssg_pace.py, test_draw_stream_visibility.py, test_framegen_resources.py, test_fsr_uniform_ownership.py, test_game_boundary.py, test_guest_holes.py, test_image_reaper_threads.py, test_image_view_retry.py, test_input_background.py, test_keyboard_option.py, test_linux_pressure.py, test_loaded_modules_drop.py, test_log_levels.py, test_menu_keys.py, test_menu_save_atomic.py, test_mesh_first_types.py, test_oot_occ_raster.py, test_option_ids.py, test_perf_send.py, test_pipeline_tour.py, test_present_acquire_ahead.py, test_present_image_free.py, test_present_source_crop.py, test_program_cache_cleanup.py, test_program_cache_one_variant.py, test_readback_timeout_reuse.py, test_recording_admission.py, test_screensaver.py, test_switch_table.py, test_target_retention.py, test_tex_desc_memo.py, test_thinlto_tls.py, test_upscaler_round_trip.py, test_vram_compact.py, test_windows_display_startup.py, test_windows_on_linux.py


Found in an AI-assisted audit of the repository. The test, the fix, the runs and this description were made with Claude Code; I reviewed them.

Co-Authored-By: Claude Opus 5.5 [email protected]

🤖 Generated with Claude Code

## Summary A power loss right after the save tool says Done can lose the save it was protecting. `copy_saves` (`scripts/longshot_saves.py`) copies the save, backs the target up, then swaps the copy in with renames, and nothing is flushed: nothing orders the copies' data before those renames. On ext4 (Linux, and the Steam Deck's drive and SD cards) a rename can reach the disk well before the data of the files it moves, so a power cut or a hang in that window can leave empty or cut files in place of the slots, and the backup the same, with the old save already moved away. `on_disk`, called right before `swap_in`, puts the copies on the disk first: `os.sync()` where Python has it (one call, every file), else each copied file's fsync (Windows). How to see it on main: nothing shows without a power cut at that moment; the test checks the order (the copies synced before the swap). Read first: `on_disk` and its call in `copy_saves`. ## Testing **Tested:** on this PC (Debian 13, ext4 on NVMe): by hand, the real tool exported a save from a game run over an emulator's (`export --longshot DIR --to DIR --backup-dir DIR`), each copy tagged in its last bytes so the two differ: the emulator's folder then held Longshot's save, the backup the emulator's old one, Done. The new test fails on main and passes here, with the rest of test_longshot_saves.py; ruff. `mise run test` (`--strict`) on main @ 1938a302, and on main with this change merged (together with #3009, #3010, #3011, #3012, #3014, from the same audit): 775 passed, 145 failed, 10 skipped both ways, so none fails only with it. The 145 are named at the end. **Still to test:** - Windows, by hand: export a save, expecting the same (there each copied file is fsynced one by one: that branch didn't run here) ## Measurements **Verdict:** an import or export takes about 8 ms more on this PC (the median), once per copy. Setup: Debian 13, ext4 on NVMe, through the PC's job queue (`timing-cpu`: nothing else running); the whole `export` command for a real save (account.sav, option.sav, one slot) over another, with a backup; main's script and this one alternating, 9 runs each. | | Before: main @ 1938a302 | After: this PR @ e8cfa476 | |---|---|---| | export, median (ms) | 46 | 54 | | export, fastest to slowest (ms) | 44 to 48 | 49 to 71 | ## Risks `os.sync()` flushes all of the system's pending writes, not only the copies': with much pending (a big copy just before, a slow SD card) Done comes a little later. The tool is run by hand, once per copy. A revert is enough. ## Checklist ### Ready for review - [x] One topic, on current `main`; the title is a Conventional Commit. - [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). - [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: a game-free test that fails on `main` and passes here. - [x] A check under `tests/` that reads changed source is updated, not weakened. - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. ### Ready to merge - [ ] Everything in Still to test has run, its results in Tested. - [ ] Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers"). - [ ] Approved in review, every comment answered. - [ ] CI passes on the head; on current `main`, measured again or a comment says why the numbers still hold. <details><summary>The 145 checks of <code>mise run test</code> that fail on main @ 1938a302 on this PC too, with or without this change</summary> 97 need a tool this PC doesn't have on PATH (`--strict` fails them): - clang (63): test_audio_output_devices.py, test_box_light_reuse.py, test_camera_centering.py, test_camera_speed.py, test_drive_target.py, test_dynres_sites.py, test_exclusive_marks.py, test_fixed_scale_sites.py, test_floor_carry_step.py, test_frame_rate_cap_step.py, test_frame_whole.py, test_free_view_anchor.py, test_fullscreen_monitor_platform.py, test_high_fps_fixes.py, test_horse_jump_step.py, test_hos_ab.py, test_hud_layout.py, test_jump_takeoff_step.py, test_linux_audio_output.py, test_loop_step.py, test_menu_hooks.py, test_microphone_test_gain.py, test_monitor_friendly_names.py, test_mouse_native.py, test_movement_speed.py, test_movie_frame_count.py, test_nvn_null_pool.py, test_nvn_tex_builder_guard.py, test_ocarina_wait.py, test_ocb_hold.py, test_oot_barrier.py, test_oot_camera_clearance.py, test_oot_cloth.py, test_oot_cloth_60.py, test_oot_cloth_time.py, test_oot_damage.py, test_oot_fov.py, test_oot_heap_retry.py, test_oot_jitter.py, test_oot_shadow.py, test_oot_shadow_fit.py, test_oot_transition_autosave.py, test_oot_voice_option.py, test_oot_wide_bubble.py, test_oot_wide_save_picture.py, test_player_update_sites.py, test_sampler_stack_walk.py, test_saved_turn_rate.py, test_scarecrow_timing.py, test_scene_format.py, test_sdl_monitor_names.py, test_shadow_slot.py, test_song_note_cue.py, test_sound_held_frames.py, test_sprint.py, test_svc_core_mask.py, test_tex_compact.py, test_text_speed.py, test_title_card_sites.py, test_water_substeps.py, test_web_floor_steps.py, test_wide_sites.py, test_wide_ui.py - spirv-val (20): f2i_immediate, global_load_16, native_suatom, test_coop_spirv.py, test_default_point_size.py, test_depth_sample_types.py, test_draw_parameters.py, test_fsr4_tune.py, test_guest_page_fast32.py, test_image_arrays.py, test_mesh_fallback_layout.py, test_mesh_unreachable_outputs.py, test_mma_software.py, test_native_float_atomic_add.py, test_nonuniform_image_arrays.py, test_spirv_struct_identity.py, test_texture_phi_dominance.py, test_unsigned_image_types.py, test_vendor_translation.py, test_wmma16_spirv_val.py - clang++ (7): test_dead_targets_sweep.py, test_lower_depbar.py, test_lower_lds_after_sts.py, test_lower_register_markers.py, test_native_suatom.py, test_program_answer_domains.py, test_target_pool_cap.py - glslang (5): test_framegen_hdr.py, test_fsr3fi_hud_layer.py, test_fsr3fi_hudless_colour.py, test_host_shaders.py, test_temporal_present_bandwidth.py - glslang, spirv-val (1): test_fsr3_shaders.py - llvm-mc (1): test_lifter.py 48 fail on main itself: test_blocks_at_start.py, test_bug_report.py, test_cloth_kernels.py, test_cloth_loops.py, test_cloth_sphere.py, test_cloth_step12.py, test_cloth_step8.py, test_cloth_step9.py, test_control_picture.py, test_crash_record.py, test_crt_options.py, test_dev_update.py, test_dlssg_pace.py, test_draw_stream_visibility.py, test_framegen_resources.py, test_fsr_uniform_ownership.py, test_game_boundary.py, test_guest_holes.py, test_image_reaper_threads.py, test_image_view_retry.py, test_input_background.py, test_keyboard_option.py, test_linux_pressure.py, test_loaded_modules_drop.py, test_log_levels.py, test_menu_keys.py, test_menu_save_atomic.py, test_mesh_first_types.py, test_oot_occ_raster.py, test_option_ids.py, test_perf_send.py, test_pipeline_tour.py, test_present_acquire_ahead.py, test_present_image_free.py, test_present_source_crop.py, test_program_cache_cleanup.py, test_program_cache_one_variant.py, test_readback_timeout_reuse.py, test_recording_admission.py, test_screensaver.py, test_switch_table.py, test_target_retention.py, test_tex_desc_memo.py, test_thinlto_tls.py, test_upscaler_round_trip.py, test_vram_compact.py, test_windows_display_startup.py, test_windows_on_linux.py </details> --- Found in an AI-assisted audit of the repository. The test, the fix, the runs and this description were made with Claude Code; I reviewed them. Co-Authored-By: Claude Opus 5.5 <[email protected]> 🤖 Generated with [Claude Code](https://claude.com/claude-code)
copy_saves copies the save, then renames the copy in and the old save out, with nothing flushed: on ext4 (the Steam
Deck's) renames reach the disk within seconds and the data much later, so a power cut can leave empty slot files and
an empty backup. The test checks the copies are synced before the swap; fails on main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fix(saves): longshot_saves.py puts the copies on the disk before the swap
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 7s
e8cfa47699
on_disk, right before swap_in: os.sync where there is one (every file at once), else each copied file's fsync
(Windows).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PedroRossi changed title from WIP: fix(saves): longshot_saves.py puts the copies on the disk before the swap to fix(saves): longshot_saves.py puts the copies on the disk before the swap 2026-12-02 16:12:20 +00:00
Author
Contributor

Tested again by hand with the two saves made different (each copy's option.sav and slot_00/caption.sav tagged in their last 8 bytes): the emulator's folder got Longshot's save and the backup the emulator's old one. The copies in the first run came from the same game save, so its byte-for-byte check could not have failed.

Tested again by hand with the two saves made different (each copy's option.sav and slot_00/caption.sav tagged in their last 8 bytes): the emulator's folder got Longshot's save and the backup the emulator's old one. The copies in the first run came from the same game save, so its byte-for-byte check could not have failed.
Owner
@review
BeanBot left a comment

Recommendation: Request changes. The copies are flushed before the swap, but the completed swap is not made durable, and Windows can silently skip failed flushes. Both cases can leave a save missing or incomplete after the tool reports success.

The tool flushes copied save data before replacing the target. This reduces one power-loss risk, but it does not yet guarantee that a completed transfer or its Windows backup survives a power loss. The Linux measurement shows an 8 ms median cost on the tested NVMe system.

Blocking (2): must change before merging.

  1. L1 scripts/longshot_saves.py:478: The flush precedes every rename, but nothing makes the resulting directory entries durable before reporting Done. (more on the line) Do: Make the swap and the relevant parent directory entries durable before deleting the old entries and reporting success, using the appropriate platform-specific durability operations and error handling.
  2. L2 scripts/longshot_saves.py:505: The Windows fallback ignores every open or fsync failure and proceeds with the swap. (more on the line) Do: Flush read-only copies with a suitable handle or safely adjust their attributes, and propagate traversal, open and flush failures so the swap is not started when durability cannot be established.

Should fix (1): not blocking, but worth doing before merging.

  • L3 tests/test_longshot_saves.py:417: The new test creates os.sync on every platform, so it never exercises the Windows per-file flush path. (more on the line) Do: Add a test that forces the no-sync path and checks that incoming and backup files are flushed before the swap, including a flush failure and a read-only copied file.

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

  • L4 scripts/longshot_saves.py:497: The timing does not cover the system-wide flush when other applications have substantial dirty data or the saves are on slow storage. (more on the line) Do: Measure an export on a Steam Deck or comparable slow storage with representative pending writes, alongside the idle case. (@fix does its part first: Flush only the copied files and required directories instead of all pending system writes.)

@fix can do L1, L2, L3 and L4.

Checks: Still running: 10 checks.

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

Does it help: Unproven as a complete durability fix. The added test checks that os.sync is called before the swap; it does not check durable directory entries or the Windows fallback. Nine interleaved exports on Debian 13 measured medians of 46 ms before and 54 ms after.

Other systems: The Linux path calls system-wide sync, including writes unrelated to saves. Windows uses per-file fsync, but its failures are ignored. The final rename durability issue affects filesystems where directory updates are not durable when the rename returns. No game rendering, audio or input code changes.

Switches: None.

Reviewed: e8cfa47699 against 1938a302fe: Read AGENTS.md, CONTRIBUTING.md, the pull request template, relevant standards, the diff and commits, the changed save-copy and test paths and their callers. Skimmed the rest of the two large changed files and save documentation. docs/review-checklist.md and the adjacent Tooling checkout were absent; recorded runs were read from the pull request and comment. No generated files or translations changed.; nothing run (a code review).

Checked and fine:

  • Both commits record AI assistance in trailers.
  • The diff contains no game files, generated assets, API version changes or switch changes.
  • The stated Linux export measurement uses alternating runs against the named base.

Testing: No commands from the project or tests were run, as requested. The author reports a successful Debian manual export and a game-free test; Windows manual testing remains outstanding, and CI is still running.

Overlaps and merge order: Not established from the available offline checkout.


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

**Recommendation: Request changes.** The copies are flushed before the swap, but the completed swap is not made durable, and Windows can silently skip failed flushes. Both cases can leave a save missing or incomplete after the tool reports success. The tool flushes copied save data before replacing the target. This reduces one power-loss risk, but it does not yet guarantee that a completed transfer or its Windows backup survives a power loss. The Linux measurement shows an 8 ms median cost on the tested NVMe system. **Blocking (2)**: must change before merging. 1. **L1** `scripts/longshot_saves.py:478`: The flush precedes every rename, but nothing makes the resulting directory entries durable before reporting Done. (more on the line) **Do:** Make the swap and the relevant parent directory entries durable before deleting the old entries and reporting success, using the appropriate platform-specific durability operations and error handling. 2. **L2** `scripts/longshot_saves.py:505`: The Windows fallback ignores every open or fsync failure and proceeds with the swap. (more on the line) **Do:** Flush read-only copies with a suitable handle or safely adjust their attributes, and propagate traversal, open and flush failures so the swap is not started when durability cannot be established. **Should fix (1)**: not blocking, but worth doing before merging. - **L3** `tests/test_longshot_saves.py:417`: The new test creates os.sync on every platform, so it never exercises the Windows per-file flush path. (more on the line) **Do:** Add a test that forces the no-sync path and checks that incoming and backup files are flushed before the swap, including a flush failure and a read-only copied file. **Testing before merging (1)** (runs on the systems it can change; it doesn't hold the recommendation) - **L4** `scripts/longshot_saves.py:497`: The timing does not cover the system-wide flush when other applications have substantial dirty data or the saves are on slow storage. (more on the line) **Do:** Measure an export on a Steam Deck or comparable slow storage with representative pending writes, alongside the idle case. *(`@fix` does its part first: Flush only the copied files and required directories instead of all pending system writes.)* `@fix` can do L1, L2, L3 and L4. **Checks:** Still running: 10 checks. <details> <summary>More: does it help, other systems, switches, what was read</summary> **Does it help:** Unproven as a complete durability fix. The added test checks that os.sync is called before the swap; it does not check durable directory entries or the Windows fallback. Nine interleaved exports on Debian 13 measured medians of 46 ms before and 54 ms after. **Other systems:** The Linux path calls system-wide sync, including writes unrelated to saves. Windows uses per-file fsync, but its failures are ignored. The final rename durability issue affects filesystems where directory updates are not durable when the rename returns. No game rendering, audio or input code changes. **Switches:** None. **Reviewed:** e8cfa47699fb against 1938a302feb0: Read AGENTS.md, CONTRIBUTING.md, the pull request template, relevant standards, the diff and commits, the changed save-copy and test paths and their callers. Skimmed the rest of the two large changed files and save documentation. docs/review-checklist.md and the adjacent Tooling checkout were absent; recorded runs were read from the pull request and comment. No generated files or translations changed.; nothing run (a code review). **Checked and fine:** - Both commits record AI assistance in trailers. - The diff contains no game files, generated assets, API version changes or switch changes. - The stated Linux export measurement uses alternating runs against the named base. **Testing:** No commands from the project or tests were run, as requested. The author reports a successful Debian manual export and a game-free test; Windows manual testing remains outstanding, and CI is still running. **Overlaps and merge order:** Not established from the available offline checkout. </details> --- <sub>Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.</sub>
@ -475,6 +475,7 @@ def copy_saves(src, dst, backup, keep_settings, dry_run):
shutil.copy2(os.path.join(src, f), os.path.join(incoming, f), follow_symlinks=False)
if not had:
os.makedirs(dst)
on_disk(incoming, *([backup] if had else []))
Member

L1 (blocking): The flush precedes every rename, but nothing makes the resulting directory entries durable before reporting Done.

When: After sync returns, swap_in moves the old slots aside and places the incoming slots, then cleanup removes the aside folder. A power cut immediately after Done can recover directory metadata from before or during those operations, leaving the target missing slots or containing an incomplete mixture even though the file contents were flushed.

Do: Make the swap and the relevant parent directory entries durable before deleting the old entries and reporting success, using the appropriate platform-specific durability operations and error handling.

**L1** (blocking): The flush precedes every rename, but nothing makes the resulting directory entries durable before reporting Done. *When:* After sync returns, swap_in moves the old slots aside and places the incoming slots, then cleanup removes the aside folder. A power cut immediately after Done can recover directory metadata from before or during those operations, leaving the target missing slots or containing an incomplete mixture even though the file contents were flushed. **Do:** Make the swap and the relevant parent directory entries durable before deleting the old entries and reporting success, using the appropriate platform-specific durability operations and error handling.
@ -491,0 +494,4 @@
find copies the disk never got (ext4 writes renames within seconds, data much later), the old save gone. POSIX:
os.sync, every file at once; Windows: each file's fsync (a read-only one is left to the system)"""
if hasattr(os, "sync"):
os.sync()
Member

L4 (should fix): The timing does not cover the system-wide flush when other applications have substantial dirty data or the saves are on slow storage.

When: An export while a large unrelated write is pending waits for os.sync to flush all mounted filesystems, so Done can be delayed by that unrelated work.

Do: Measure an export on a Steam Deck or comparable slow storage with representative pending writes, alongside the idle case.

**L4** (should fix): The timing does not cover the system-wide flush when other applications have substantial dirty data or the saves are on slow storage. *When:* An export while a large unrelated write is pending waits for os.sync to flush all mounted filesystems, so Done can be delayed by that unrelated work. **Do:** Measure an export on a Steam Deck or comparable slow storage with representative pending writes, alongside the idle case.
@ -491,0 +502,4 @@
try:
with open(os.path.join(folder, name), "rb+") as f:
os.fsync(f.fileno())
except OSError:
Member

L2 (blocking): The Windows fallback ignores every open or fsync failure and proceeds with the swap.

When: A copied read-only save retains its read-only attribute through copy2, so opening it with rb+ can fail on Windows. Likewise, an actual fsync I/O error is swallowed: the tool removes the old target and prints Done although the incoming file or backup was never confirmed on disk.

Do: Flush read-only copies with a suitable handle or safely adjust their attributes, and propagate traversal, open and flush failures so the swap is not started when durability cannot be established.

**L2** (blocking): The Windows fallback ignores every open or fsync failure and proceeds with the swap. *When:* A copied read-only save retains its read-only attribute through copy2, so opening it with rb+ can fail on Windows. Likewise, an actual fsync I/O error is swallowed: the tool removes the old target and prints Done although the incoming file or backup was never confirmed on disk. **Do:** Flush read-only copies with a suitable handle or safely adjust their attributes, and propagate traversal, open and flush failures so the swap is not started when durability cannot be established.
@ -413,0 +414,4 @@
# (renames reach the disk within seconds, a copy's data much later on ext4: a power cut in between left the
# target's slots and its backup empty, and the old save gone)
order, real_swap = [], oot.swap_in
with mock.patch.object(oot.os, "sync", create=True, side_effect=lambda: order.append("sync")), \
Member

L3 (should fix): The new test creates os.sync on every platform, so it never exercises the Windows per-file flush path.

When: On Windows the mock makes hasattr(os, "sync") true, bypassing all file opens and fsync calls. Removing the fallback or swallowing all its errors would still pass this test.

Do: Add a test that forces the no-sync path and checks that incoming and backup files are flushed before the swap, including a flush failure and a read-only copied file.

**L3** (should fix): The new test creates os.sync on every platform, so it never exercises the Windows per-file flush path. *When:* On Windows the mock makes hasattr(os, "sync") true, bypassing all file opens and fsync calls. Removing the fallback or swallowing all its errors would still pass this test. **Do:** Add a test that forces the no-sync path and checks that incoming and backup files are flushed before the swap, including a flush failure and a read-only copied file.
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 7s
This pull request doesn't have enough approvals yet. 0 of 1 approvals granted.
Some workflows are waiting to be reviewed.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/save-tool-flush:PedroRossi-fix/save-tool-flush'
git switch 'PedroRossi-fix/save-tool-flush'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 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!3013
No description provided.