Skip to content

fix(lock): stop handing one lock to two processes when its owner exits mid-check - #1699

Merged
SaulMoro merged 1 commit into
Tencent:mainfrom
hz89Dev:fix/lock-reread-before-stale
Nov 3, 2026
Merged

SaulMoro merged 1 commit into
Tencent:mainfrom
hz89Dev:fix/lock-reread-before-stale

Conversation

@hz89Dev

@hz89Dev hz89Dev commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Two teamai processes could both hold the same lock (sync, reports, learnings, secrets, update). lockState read 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. Now lockState reads 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 read

The trace that broke it (4 processes, no crash):

  1. p1 holds the lock and starts releaseLock.
  2. p2's create fails, and it reads p1's payload.
  3. p1 removes the lock and exits.
  4. p2's kill(p1) returns ESRCH, so p2 judges the lock stale and takes the sentinel.
  5. p3 creates the lock. p2's second createOrInspect reads p3's payload.
  6. p3 releases the lock and exits. p4 creates the lock.
  7. p2's kill(p3) returns ESRCH, so p2 renames over p4's live lock.
  8. p2 and p4 both return true.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature causing existing behavior to change)
  • Documentation only
  • Refactor / internal cleanup

Test Plan

  • npx tsc --noEmit passes
  • npm run lint passes
  • npx vitest run passes: ran the 10 files that use the real lock or lockState (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.
  • Added/updated tests for the change

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.

  • Before: expected true to be false. acquireLock took over the live lock.
  • After: passes. acquireLock returns 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 each process.kill is 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, then node dist/index.js env unset <NAME> --global against ~/.teamai/secrets/machine.json.lock):

== 1. lock held by a dead pid (999999): expect reclaim and success
✔ Removed TEST_TOKEN's global value (every team on this machine) (.../home/.teamai/secrets/machine.json).
exit=0
lock left behind: none
== 2. lock held by a live pid: expect busy and lock untouched
✖ Another teamai command is changing .../home/.teamai/secrets/machine.json. Run the command again once it has finished. Nothing was changed.
exit=1
lock after: {"pid":13157,"owner":"live","startedAt":"x"}

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

  • The re-read also applies to the sentinel's staleness check in acquireReclaimSentinel, since that check goes through lockState. There, missing and live both mean "not stale", which is the same answer as before.
  • This does not close the residual race the acquireLock comment names: a reclaimer that dies while holding the sentinel can still let two survivors take it. The model still finds that one.
  • Merge danger: two-way door. The only behaviour change is a second read after ESRCH. Blast radius: every caller of acquireLock. The worst case is a lock reported busy that would have been reclaimed, and the next attempt reclaims it.

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).
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

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 SaulMoro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@SaulMoro
SaulMoro merged commit b656d1b into Tencent:main Nov 3, 2026
18 checks passed
SaulMoro pushed a commit that referenced this pull request Nov 4, 2026
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.
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.

3 participants