Skip to content

engine: close the AutoRecover temporary file before the rename - #2286

Open
tomaszstaniak wants to merge 1 commit into
storytold:mainfrom
tomaszstaniak:fix/recovery-close-before-rename
Open

tomaszstaniak wants to merge 1 commit into
storytold:mainfrom
tomaszstaniak:fix/recovery-close-before-rename

Conversation

@tomaszstaniak

@tomaszstaniak tomaszstaniak commented Nov 20, 2026 •

Copy link
Copy Markdown

Fixes #2285.

recovery::write_atomic now closes the temporary file before renaming it over the recovery entry, the way io::write_file already does. On a system whose rename needs the source to itself (AROS: its C library replaces an existing destination only when it can lock the source exclusively), an update of an existing AutoRecover entry fails with "File exists" (the same pattern broke saving in a GridCraft port there). The temporary file is still removed when the rename fails; nothing else changes.

Testing:

  • cargo test -p deckcraft-engine: 614 passed, 0 failed (macOS). The 6 recovery tests pass.
  • cargo clippy -p deckcraft-engine --all-targets: nothing in recovery.rs.
  • The same change is in my AROS port of DeckCraft 0.18.1, whose scripted sessions pass on AROS x86_64 and AArch64; those sessions did not exercise an AutoRecover update of an existing entry.

No new test: on Linux, macOS and Windows a rename over an existing file succeeds with the source still open, so a test cannot fail without the fix on the platforms CI runs. I could not find a way to observe the open handle at the moment of the rename without adding a hook to production code only for the test; I can add one if you prefer that.

Summary by CodeRabbit

  • Bug Fixes
    • File updates now replace the destination only after the new data has been fully written and synchronized. If writing or synchronization fails, the existing destination remains unchanged and an error is reported, helping prevent incomplete updates from appearing as successful saves.

write_atomic renamed the temporary file over the recovery entry while the file was still open. On a system whose rename needs the source to itself, such as AROS, replacing an existing entry then fails with "File exists" and the entry is not updated. The file is now closed first, as io::write_file already does; the temporary file is still removed when the rename fails.
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

write_atomic now closes the temporary file after writing and syncing, and before renaming it to the recovery destination. Write, sync, and rename errors continue through the existing cleanup and error path.

Changes: AutoRecover atomic write

Layer / File(s) Summary
Close temporary file before rename
crates/engine/src/recovery.rs
write_atomic stores the write-and-sync result, closes the temporary file, and then attempts the rename. Errors continue through the existing cleanup and error path.

Estimated code review effort: 2 (Simple) | ~5 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: dylanmtaylor, echelon

Merge Risk: Minimal · up to 89b1e

This change closes the temporary AutoRecover file before renaming it, which matches how the existing file-writing code behaves. It carries no identified risk on current platforms and should be safe to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "engine: close the AutoRecover temporary file before the rename" names the main change. The raw summary says write_atomic now closes the temporary file before renaming it to the destination,…
Linked Issues check Passed Issue #2285 asks that recovery::write_atomic close its temporary file before renaming it over the recovery entry, as io::write_file does. The whole-PR diff (merge base to head) changes only `crate…
Out of Scope Changes check Passed The whole-PR diff contains one hunk in crates/engine/src/recovery.rs, +6/-1 lines, inside write_atomic. The change reorders the close and the rename and adds a comment that explains the reason. Ev…
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title "engine: close the AutoRecover temporary file before the rename" names the main change. The raw summary says write_atomic now closes the temporary file before renaming it to the destination, and the objectives describe the same change to recovery::write_atomic for AutoRecover entries. The title is specific and matches the changeset.

Full details: Linked Issues check

Explanation

Issue #2285 asks that recovery::write_atomic close its temporary file before renaming it over the recovery entry, as io::write_file does. The whole-PR diff (merge base to head) changes only crates/engine/src/recovery.rs. The new code runs file.write_all(bytes).and_then(|()| file.sync_all()) into written, then calls drop(file), then runs written.and_then(|()| std::fs::rename(&tmp, path)). The rename therefore runs after the handle is closed. A write or sync error still skips the rename. The map_err branch still removes the temporary file and returns an error naming the destination, so the failure path is unchanged. The issue does not request an automated test. The PR states that no test was added and gives its reason. That omission does not fail this requirement. Scope: the issue has no other coding requirement for this change.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff contains one hunk in crates/engine/src/recovery.rs, +6/-1 lines, inside write_atomic. The change reorders the close and the rename and adds a comment that explains the reason. Every line serves issue #2285. No unrelated file or behavior change appears in the diff. The PR description says that nothing else changes, and the diff confirms it.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

Clippy (1.99.1)

Clippy execution failed
Clippy (1.99.1) exceeded its 180-second time budget. Findings from completed work are retained; remaining checks are incomplete.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 6ebac8b and 89b1e75.

Files selected for processing: 1

  • crates/engine/src/recovery.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@autocraft-bot autocraft-bot Bot added the bugfix Fixes something that is broken: wrong result, crash, data loss label Nov 21, 2026

@autocraft-bot autocraft-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-generated review (Claude Opus 5.5). Not a maintainer decision.

No blocking problems found.

write_atomic now closes the temporary file before renaming it over the recovery entry, as io::write_file already does, and keeps the cleanup when the write or the rename fails.

  • crates/engine/src/recovery.rs:98-103: the write result is kept, the file is dropped, then the rename runs only if the write and sync succeeded. This is the same order as crates/engine/src/io.rs:62-64.
  • crates/engine/src/recovery.rs:104: if the write, sync or rename fails, the temporary file is still removed. It is now closed before removal, which also helps on platforms that will not delete an open file.
  • Error messages are unchanged: a write, sync or rename failure is still reported against the entry path.
  • The settings writer (crates/ui-egui/src/settings.rs:44) uses std::fs::write, which closes the file before its rename, so no other write-then-rename path needs the same change.

Claims checked:

  • verified: write_atomic closes the temporary file before renaming it, the way io::write_file does (crates/engine/src/recovery.rs:98 keeps the write/sync result, :102 drops the file, :103 renames only afterwards. crates/engine/src/io.rs:62-64 uses the same order (written, drop(file), rename).)
  • verified: The temporary file is still removed when the rename fails (crates/engine/src/recovery.rs:103-105: map_err on the combined write/sync/rename result calls std::fs::remove_file(&tmp) before returning the error.)
  • verified: Nothing else changes (The diff only touches crates/engine/src/recovery.rs:98-103. The open options (:90-97), error text (:105) and callers (save at :158/:160, open at :226) are unchanged.)
  • verified: Fixes issue 2285: updating an existing AutoRecover entry fails on systems whose rename needs exclusive access to the source (crates/engine/src/recovery.rs:102 drops the handle before the rename at :103, which removes the open-source-during-rename pattern the issue describes. The AROS behaviour itse…

Reviewed at 89b1e75.

Recommendation: merge, after a maintainer's decision.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Fixes something that is broken: wrong result, crash, data loss

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AutoRecover renames its temporary file while it is still open

1 participant