Repository navigation
fix(lock): stop two processes sharing a lock after a reclaimer dies holding the sentinel - #1701
Conversation
…e one When a process died holding the reclaim sentinel, the next reclaimers took it over by renaming it away. A rename moves whatever file has the name by then: two reclaimers could both judge the dead sentinel stale, the first rename it away and create its own, and the second then rename the first one's live sentinel away and create a third. Both went on to rename over the stale lock, and both held it (the window the acquireLock comment named, Tencent#760). A dead sentinel is now removed under an exclusive claim <sentinel>.steal-<dead owner>. An owner token is never written again, so while one process holds that claim no other can remove the dead file, and the holder re-reads the file and removes it only if it still names the dead owner. A claim whose holder died too is removed the same way, one level deeper, before it is taken. Migration skips the claims like the other lock artifacts. The new lock-atomic test stages the race and fails on the old code (acquireLock returned true after moving the live sentinel); removing the re-read under the claim makes it fail again.
|
No confident findings in the specified diff. The PR description documents sufficient testing: build, typecheck, lint, focused unit tests, and two representative real-CLI recovery runs. No additional provider/agent matrix is required. Review was read-only; I did not run, build, or install anything from the PR. |
SaulMoro
left a comment
There was a problem hiding this comment.
This was generated by AI during triage.
The claim closes the window the trace shows. removeDeadFile removes a sentinel only while holding <sentinel>.steal-<dead owner>, and only after a re-read under that claim still names the dead owner. Because an owner token is never written again, once the re-read matches, the file can only be the dead owner's: a new sentinel cannot be created while it exists, and no other process can remove it without the same claim. So the remove can no longer hit a live sentinel the way the old rename did. A dead claimant's claim is handled by the same function one level down, and removeIfOwner in the finally drops the claim on every path. lockState keeps its signature, the migration pattern covers the nested .steal- names, and a failed remove now rejects instead of reading as contention, matching #999.
The new race test stages step 4 and failed before the change, and the real-CLI record shows both dead-sentinel shapes recovered. The notes on mixed versions, leftover claims and the depth chain match the code, and I agree with leaving a depth cap out for now.
I agree with the CI review: no findings. Approving; it merges once CI is green.
Summary
Two teamai processes could both hold the same lock after a reclaimer died while holding the reclaim sentinel. The next reclaimers took a dead sentinel over by renaming it away, and a rename moves whatever file has the name by then. So the second reclaimer could rename away the live sentinel the first had just created. Now a dead sentinel is removed under an exclusive claim on its dead owner, and only while it still names that owner.
The trace that broke it (4 processes; the window the
acquireLockcomment named, #760):<lock>.sentinel.renamemoves p4's live sentinel away, and p3 creates another.An owner token (
randomUUID()) is never written again. So while one process holds<sentinel>.steal-<dead>, no other process can remove the dead file. The holder's re-read turns away a slow process that confirmed the dead owner earlier. Only UUID owner tokens go into a file name.lockState's body moved intoinspectLock, which also returns the dead holder;lockStatekeeps its signature. Migration skips the claim files like the other lock artifacts.Type of Change
Test Plan
npm run typecheckpassesnpm run lintpassesnpx vitest runpasses: ran the 11 files that use the real lock orlockState(lock-atomic,update,dry-run-load-path,migrate,score-sessions,reports-branch-readonly,maintenance-paths,reports-reader-lock,import-lock,workflow-skill-lock,doctor); 419 passed. Full suite left to CI.New race test (
lock-atomic, "does not take a live sentinel another reclaimer created after a dead one was confirmed"). It stages step 4: right after the re-read confirms the dead sentinel, another reclaimer replaces it with its own live one.expected true to be false.acquireLockmoved the live sentinel and took the lock.Other new tests:
The old rename-failure test went away with the rename. The migration artifact test lists the claim names.
Model check. I checked the design in a TLA+ model of
acquireLock/releaseLock(each filesystem call and eachprocess.killis one step, with every interleaving tried):Real CLI (
npm run build, thennode dist/index.js env unset TEST_TOKEN --globalagainst~/.teamai/secrets/machine.json.lock):The race itself cannot be triggered on demand from the CLI. The unit test and the model cover it. The lock does not depend on the provider or the agent, so I ran no provider or agent matrix.
Related Issues
Follows #760 and #1699.
Notes for Reviewers
.reclaim-<uuid>files, and does not affect correctness.randomUUID(), so the only such file is a half-written O_EXCL one, which already read as held.acquireLock, but only on the path where a dead process left the sentinel behind.