Repository navigation
write_mask: roll back when the caller's provenance step fails (restore an overwritten mask) - #397
Open
guijacquemet wants to merge 3 commits into
Open
guijacquemet wants to merge 3 commits into
guijacquemet wants to merge 3 commits into
Conversation
Under the type's write lock, an overwrite first copies the earlier mask to a hidden .<name>.previous.<random> beside it. If on_published raises (any BaseException), the earlier mask is moved back byte for byte, or a new mask is removed, still under the lock, then the error propagates. A rollback that cannot restore or remove raises SegmentationError naming both files and keeps the copy. The record's duplicate-image check uses a seen-set. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Scma6W3LC11s3qjXHZuGDm
…ckups An interrupt after os.replace but before _write_and_publish returned dropped the backup while the new mask stayed. The target's inode now decides whether the publish happened. stray_backups() reports leftover .previous copies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Scma6W3LC11s3qjXHZuGDm
…ynced The write_mask docstring and contract 15.5 now say that the rollback is unconditional (a callback that commits must reconcile its own post-commit failures) and that the record is replaced atomically but not fsynced. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Scma6W3LC11s3qjXHZuGDm
guijacquemet
marked this pull request as ready for review
November 22, 2026 18:05
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.
What
write_mask: under the type's write lock, anoverwrite=Truewrite first copies the existing mask to a hidden.<name>.previous.<random>beside it (the temp-name pattern; a dot-file, which the masks discovery skips viais_system_name).on_published(path)is still called under the lock. If it raises (anyBaseException, includingKeyboardInterrupt), the write is rolled back under the same lock: the backup is moved back over the target withos.replace(same bytes and mode), or the new mask is removed when there was no earlier one. Then the error is raised again. If the restore or the removal fails, the result is aSegmentationErrorthat names both files, and the backup is kept. When the callback succeeds, the backup is removed.segmentation_record._batch: the duplicate-image check now uses a seen-set, so it runs in linear time (it usedlist.countonce per entry).Why
External audit of #375 (22 November 2026), blockers 1–3. Rollback has to happen where the lock is held. Before this change, a failed overwrite left the new mask published, and the caller's own rollback could only remove that new mask, so the earlier valid mask was lost. With this change #375 can write its record inside
on_publishedand drop its own_withdraw.Tests
tests/test_segmentation_rollback.py(new). The old test "raising callback leaves the mask published" is removed because that behaviour is now intentionally different.images_to_segment.os.replacefails during the restore (monkeypatched), the error is loud, names both paths, keeps the backup with the earlier bytes, and keeps the caller's error in the chain.All 7 fail on 7a0a521
src/. With this change: 92 passed (rollback, write_lock, handoff and record tests).tools/check.py --rulesand ruff are clean.Generated by Claude Code