fix(storage): make save and export crash-safe (fixes #282)#296
Open
sshekhar563 wants to merge 1 commit into
Open
fix(storage): make save and export crash-safe (fixes #282)#296sshekhar563 wants to merge 1 commit into
sshekhar563 wants to merge 1 commit into
Conversation
Replace delete-before-move with backup-then-swap pattern. Adds: - _replace_with_backup(): atomic swap with rollback on failure - write_bytes_atomic(): temp file + fsync + atomic replace for exports - _fsync_directory(): platform-aware dir fsync (no-op on Windows) - Regression tests for failed replacements and temp cleanup
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.
Description
Fixes #282 — Soul save and export are not crash-safe; a crash can destroy the only copy.
The Bug
save_soul_full()andsave_soul_flat()usedshutil.rmtree()/unlink()to delete the existing soul directory before moving new data into place. A crash between the delete and the move destroys the only readable copy.Soul.export()usedPath.write_bytes()directly, which truncates the file on open — a mid-write crash leaves a corrupt, partial archive.The Fix
save_soul_full()rmtree→move_replace_with_backupsave_soul_flat()delete→moveSoul.export()Path.write_bytes()write_bytes_atomic()(temp + fsync + swap)New crash-safe helpers in
storage/file.py:_replace_with_backup(source, target)— moves existing target to.bak, swaps source into place, restores.bakif swap failswrite_bytes_atomic(path, data)— writes to temp file,fsync, atomic replace via_replace_with_backup_fsync_directory(path)— platform-aware dir fsync (no-op on Windows whereos.open()on dirs raisesPermissionError)_fsync_tree(path)— fsyncs all files and directories under a generated soul directoryTests Added