Repository navigation
fix(lock): stop handing one lock to two processes when its owner exits mid-check - #1699
Conversation
lockState read the lock file, then checked its pid in a separate step. If the owner released the lock and exited between the two, and another process created a fresh lock, the fresh live lock was judged stale. A reclaimer holding the sentinel then renamed over it, and both processes held the lock. After the pid check reports the owner gone, lockState now reads the file again: only the same dead owner's file is stale, a vanished file is missing (createOrInspect makes one more exclusive create), and any other content is live. The added lock-atomic test stages that interleaving and fails without the fix (acquireLock returned true over the live lock).
|
No findings in the scoped diff. The re-read prevents a replaced lock from being classified as stale, and the regression test targets that race. The PR description documents sufficient testing, including lint, targeted unit tests, and a representative real-CLI run after building. No additional provider/agent matrix is required. Review was read-only; I did not run, build, or install PR code. |
SaulMoro
left a comment
There was a problem hiding this comment.
This was generated by AI during triage.
The re-read closes the race the trace describes: a lock is stale only while it is still the dead owner's own file, and a lock that vanished goes back through createOrInspect's one extra exclusive create. The remaining step, between the re-read and the reclaimer's rename, is safe: a stale file can only be removed by its owner, who is gone, other processes' creates fail with EEXIST while it exists, and reclaimers are serialized by the sentinel. The new lock-atomic.test.ts case stages exactly step 7 and failed before the change; the real-CLI record shows a dead owner's lock reclaimed and a live one left alone.
I agree with the CI review: no findings. Approving.
A reclaimer that finds the reclaim sentinel held by a dead process no longer renames it away, since a rename moves whatever file has the name by then, including a live sentinel another reclaimer has just created. The dead sentinel is now removed under an exclusive `<sentinel>.steal-<dead owner>` claim, and only while a re-read under that claim still names the dead owner; a dead claimant's claim is handled the same way one level down. Migration skips the claim files like the other lock artifacts. Follows #760 and #1699.
Summary
Two teamai processes could both hold the same lock (sync, reports, learnings, secrets, update).
lockStateread the lock file and then checked its pid as two separate steps. If the owner released the lock and exited between them, and a third process created a fresh lock, that fresh live lock was judged stale. A reclaimer holding the sentinel then renamed over it. NowlockStatereads the file again after the pid check, and treats the lock as stale only if it is still the dead owner's file.lockState(lock) content = read(lock) if kill(content.pid, 0) succeeds or EPERM return live - return stale + current = read(lock) + if ENOENT return missing # createOrInspect makes one more exclusive create + if current == content return stale + return live # a new holder made it after our readThe trace that broke it (4 processes, no crash):
releaseLock.kill(p1)returns ESRCH, so p2 judges the lock stale and takes the sentinel.createOrInspectreads p3's payload.kill(p3)returns ESRCH, so p2 renames over p4's live lock.Type of Change
Test Plan
npx tsc --noEmitpassesnpm run lintpassesnpx vitest runpasses: ran the 10 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); 330 passed. Full suite left to CI.New test (
lock-atomic.test.ts, "under the reclaim sentinel, does not rename over a lock re-created while the old owner's pid was checked"). It stages step 7: the pid check of the old owner swaps in a live lock owned by another process, then reports ESRCH.expected true to be false.acquireLocktook over the live lock.acquireLockreturns false, and the lock still names the other owner.Model check. I checked these steps in a TLA+ model of
acquireLock/releaseLock(each filesystem call and eachprocess.killis one step, with every interleaving tried). Before the fix, the model finds the trace above at 4 processes. With the fix, it finds no violation at 4 or 5 processes with exits, or at 3 processes with crashes and exits. With the re-read removed, it fails again.Real CLI (
npm run build, thennode dist/index.js env unset <NAME> --globalagainst~/.teamai/secrets/machine.json.lock):The lock does not depend on the provider or the agent, so I ran no provider or agent matrix.
Related Issues
Follows #760.
Notes for Reviewers
acquireReclaimSentinel, since that check goes throughlockState. There,missingandliveboth mean "not stale", which is the same answer as before.acquireLockcomment names: a reclaimer that dies while holding the sentinel can still let two survivors take it. The model still finds that one.acquireLock. The worst case is a lock reported busy that would have been reclaimed, and the next attempt reclaims it.