fix(state): never offer a half-written backup as a restore point - #43
Merged
Conversation
Found reviewing #42. sqlite3.connect creates the destination file before connection.backup() runs, and backup_ledger had no cleanup on the failure path — so a backup interrupted by a full disk, an I/O error, or a killed process left a 0-byte file carrying the real ...-pre-migration.sqlite name. That file is the newest, so latest_pre_migration_backup selected it and curator doctor printed it as the ledger to restore. Following that advice copies an empty database over a live ledger and destroys it — on exactly the disaster path this code exists to protect. Reproduced before fixing: with a good 290KB backup already on disk, one failed attempt left a 0-byte sibling and latest_pre_migration_backup returned the 0-byte one. Backups are now written to a .partial name the restore glob does not match and renamed into place only after the copy completes, so an incomplete backup can never carry the real name — this covers the killed-process case too, which cleanup in an except block would not. The partial is removed on failure, and an unusable file is skipped when choosing what to advertise. Co-Authored-By: Claude Opus 5 (1M context) <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.
Found by reviewing #42. A real defect in that change, on the disaster path the feature exists to protect.
The bug
sqlite3.connectcreates the destination file beforeconnection.backup()runs, andbackup_ledgerhad no cleanup on the failure path. A backup interrupted by a full disk, an I/O error, or a killed process therefore left a 0-byte file carrying the real…-pre-migration.sqlitename.That file is the newest, so
latest_pre_migration_backupselected it andcurator doctorprinted it as the ledger to restore. Following that advice copies an empty database over a live ledger and destroys it.Reproduced before fixing — a good 290KB backup on disk, then one failed attempt:
The fix
Write to a
.partialname the restore glob does not match, and rename into place only once the copy completes. Rename is atomic, so an incomplete backup can never carry the real name — which also covers the killed-process case that cleanup in anexceptblock would miss. The partial is removed on failure.Plus one guard on the advice path: a zero-byte file is never a usable ledger, so it is skipped when choosing what to advertise. That is deliberate belt-and-braces on a code path whose output is "copy this over your live ledger".
Verification
backup.py).The other four review dimensions — CLAUDE.md adherence, git history, prior PR comments, code-comment invariants — returned no findings.
🤖 Generated with Claude Code