Skip to content

fix(ledger): lock appends so concurrent writers cannot fork the chain - #7

Merged
bhyi4 merged 1 commit into
mainfrom
claude/ledger-append-lock
Nov 24, 2026
Merged

bhyi4 merged 1 commit into
mainfrom
claude/ledger-append-lock

Conversation

@bhyi4

@bhyi4 bhyi4 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

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 reports prev_seal broken — deletion/insertion/reorder detected on an honest ledger. Reproduced on main (a4f1c10) with 4 processes × 50 record() calls.

Change

  • The read-head-then-append in _seal() (used by record() and witness_peer()) now runs under an exclusive OS lock on a sidecar <ledger>.lock.
  • The lock uses fcntl on POSIX and msvcrt on Windows, with a 60 s timeout that raises TimeoutError. 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.
  • Seal format unchanged. .gitignore now ignores *.jsonl.lock. Added a CHANGELOG entry under Unreleased.

Tests

  • New tests/test_concurrent_append.py:
    • 4 real subprocesses × 30 record() calls must leave one intact chain of 120 entries.
    • The lock is released after a failed append.
    • Both tests fail with am.py reverted to main.
  • Full suite: 88 passed locally on Python 3.13, Linux. The Windows branch is not tested.

Generated by Claude Code

_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
@bhyi4 bhyi4 self-assigned this Nov 24, 2026
@bhyi4
bhyi4 merged commit 2fbd795 into main Nov 24, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants