Repository navigation
fix(ledger): lock appends so concurrent writers cannot fork the chain - #7
Merged
Merged
Conversation
_seal() read the head seal and then appended a line pointing at it, with no lock. Two processes could read the same head and both append after it, and verify_chain() reported the honest second line as a deletion/insertion (reproduced with 4 processes x 50 record() calls). The read-head-then-append now runs under an exclusive OS lock on <ledger>.lock. Seal format unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Aq8kkjKAMDjSSK54hcWnuc
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.
Requested by 범후연이 · project thread
Problem
_seal()reads the head seal and then appends a line pointing at it, with no lock between the two steps. If two processes append at the same time, both can point at the same head.verify_chain()then reportsprev_seal broken — deletion/insertion/reorder detectedon an honest ledger. Reproduced on main (a4f1c10) with 4 processes × 50record()calls.Change
_seal()(used byrecord()andwitness_peer()) now runs under an exclusive OS lock on a sidecar<ledger>.lock.fcntlon POSIX andmsvcrton Windows, with a 60 s timeout that raisesTimeoutError. It is the same lock as measure-mirror's companion PR. The code is copied rather than shared because both packages are stdlib-only and independent..gitignorenow ignores*.jsonl.lock. Added a CHANGELOG entry under Unreleased.Tests
tests/test_concurrent_append.py:record()calls must leave one intact chain of 120 entries.am.pyreverted to main.Generated by Claude Code