Skip to content

fix(lock): stop two processes sharing a lock after a reclaimer dies holding the sentinel - #1701

Merged
SaulMoro merged 1 commit into
Tencent:mainfrom
hz89Dev:fix/lock-sentinel-takeover
Nov 4, 2026
Merged

SaulMoro merged 1 commit into
Tencent:mainfrom
hz89Dev:fix/lock-sentinel-takeover

Conversation

@hz89Dev

@hz89Dev hz89Dev commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

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 acquireLock comment named, #760):

  1. p1 dies holding the lock, and p2 dies holding <lock>.sentinel.
  2. p3 and p4 both read the sentinel, see p2 is dead, and confirm it with the re-read.
  3. p4 renames the sentinel away and creates its own.
  4. p3's rename moves p4's live sentinel away, and p3 creates another.
  5. p3 and p4 both see p1's lock as stale, both rename over it, and both return true.
 acquireReclaimSentinel(sentinel)
   exclusiveCreate(sentinel) → held
-  lockState(sentinel) == stale
-    rename(sentinel, sentinel.reclaim-<me>)   # moves whatever is there now
+  removeDeadFile(sentinel)
+    dead = owner of sentinel, judged stale (read, pid check, re-read)
+    exclusiveCreate(sentinel.steal-<dead>)   # one claimant per dead owner
+      held by a dead claimant → removeDeadFile(that claim), then create it
+    re-read sentinel; remove it only if it still names <dead>
+    removeIfOwner(claim)
   exclusiveCreate(sentinel)

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 into inspectLock, which also returns the dead holder; lockState keeps its signature. Migration skips the claim files like the other lock artifacts.

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

  • npm run typecheck passes
  • npm run lint passes
  • npx vitest run passes: ran the 11 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, doctor); 419 passed. Full suite left to CI.
  • Added/updated tests for the change

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.

  • Before: expected true to be false. acquireLock moved the live sentinel and took the lock.
  • After: passes. The live sentinel and the stale lock are left alone.
  • With the re-read under the claim removed: fails again.

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 each process.kill is one step, with every interleaving tried):

  • The rename design breaks mutual exclusion at 4 processes with crashes.
  • The claim design shows no violation at 4 processes (with crashes, and with crashes and exits), or at 5 processes with crashes (49.6M distinct states). That last scope reaches a second-level claim.
  • Removing the re-read under the claim breaks it again.

Real CLI (npm run build, then node dist/index.js env unset TEST_TOKEN --global against ~/.teamai/secrets/machine.json.lock):

== 1. dead lock + sentinel left by a dead reclaimer
before: machine.json machine.json.lock machine.json.lock.sentinel
✔ Removed TEST_TOKEN's global value (every team on this machine) (<sandbox>/home/.teamai/secrets/machine.json).
exit=0
after:  machine.json
== 2. dead lock + dead reclaimer's sentinel + dead claimant's claim
before: machine.json machine.json.lock machine.json.lock.sentinel machine.json.lock.sentinel.steal-11111111-1111-4111-8111-111111111111
✔ Removed TEST_TOKEN's global value (every team on this machine) (<sandbox>/home/.teamai/secrets/machine.json).
exit=0
after:  machine.json

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

  • Mixed versions. An older teamai still takes a dead sentinel over by renaming it. Between two old processes that race already existed, and the old code never sees the claim files, so running both versions together is no worse than before.
  • Leftover claims. A claim stays behind if its holder is killed after removing the dead sentinel. No one needs it again, so it is litter, like the old .reclaim-<uuid> files, and does not affect correctness.
  • Ownerless sentinels. A sentinel whose owner is not a UUID is never taken over. Every version that writes a sentinel uses randomUUID(), so the only such file is a half-written O_EXCL one, which already read as held.
  • Deep chains. Each crash that happens while holding the deepest claim makes the chain one level longer. After about five in a row, the file name would pass 255 bytes, and recovery would need the files removed by hand. I left a depth cap out as unneeded for now.
  • Merge danger. Two-way door. Blast radius: every caller of acquireLock, but only on the path where a dead process left the sentinel behind.

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

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

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 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 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.

@SaulMoro
SaulMoro merged commit 93aac81 into Tencent:main Nov 4, 2026
18 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.

3 participants