Repository navigation
fix(state): fsync state.json before atomic rename and clean up temp file - #250
Merged
Merged
Conversation
save_state wrote a temp file and renamed it over state.json, but never synced the temp file first. On ext4 (delalloc) or APFS a crash or power loss after the rename can persist the rename before the data, leaving an empty or truncated state.json and a session that fails to load. A failed write or rename also left state.json.tmp.<pid> behind. Write through File + sync_all before the rename, and remove the temp file on failure. Adds tests for overwrite + no leftover temp, and for cleanup when the rename fails. Ratchets clippy::mem_forget to deny per the pre-push checklist. Opened by the moadim routine "Polyhook/steplock production-readiness audit → PR + Slack". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
save_state(core/src/state.rs) wrotestate.json.tmp.<pid>and renamed it overstate.json, but never synced the temp file. On filesystems with delayed allocation (ext4, APFS), a crash or power loss can persist the rename before the data. The result is an empty or truncatedstate.json, and the session fails to load. A failed write or rename also left the temp file behind.Change
File+sync_all()beforefs::rename.mv"; it isfs::rename).clippy::mem_forgettodeny(pre-push checklist convention).Unreleased > Fixedentry.Verification
cargo fmt --check,cargo clippy --all-targets --locked -- -D warnings, andcargo test --lockedpass locally (193 tests).🤖 Generated with Claude Code