Repository navigation
Conversation
Both LockManager and CheckoutLock recovered a stale lock via unlink(lockPath) then openSync(wx) — two syscalls with a window in which two sessions that both classified the same lock stale could each destroy the other's fresh lock and both win (two holders). Route the steal through one shared stealStaleLock helper (alongside the already-shared classifyStaleness). It takes possession atomically with rename(lockPath -> tmp) and verifies identity: it discards only the stale inode it classified, and links a fresh lock that appeared in the window back into place rather than destroying it. Win or lose, acquire falls through to the terminal O_CREAT|O_EXCL create, which arbitrates the real holder. Deviation from the spec's fix sketch: a plain rename-away is behaviorally identical to the unlink it replaces (rename moves whatever is at the path, fresh or not) — verified by hand-driven interleave. The identity check is what makes AC-1 hold and AC-4 mutation-verifiable. A narrow residual (a third session's create racing the restore) is documented for follow-up; it never destroys a live lock. Tests: white-box interleave tests in both lock suites driving the documented A.steal -> A.create -> B.steal -> B.create ordering. Entire-Checkpoint: 38d0311676e9
…n sweep QA on PR #913 surfaced three non-blocking gaps; this fills all three: 1. Wiring tests (both classes): the helper tests proved stealStaleLock, but reverting either acquire() stale branch to a blind unlinkSafe failed no test. New per-class tests inject the isPidAlive probe to claim the path inside the classification->steal window; only a real CAS at the call site refuses to destroy that claim. 2. stealStaleLock never throws: rename errors degrade to a lost steal (the terminal O_EXCL create surfaces real environment problems with the same errno the pre-#908 path did), and a link-capability failure (EPERM/ENOTSUP) falls back to rename-restore — leaving the fresh lock renamed-away IS the two-holder bug, so restoring outweighs link's no-overwrite guarantee on such filesystems. Test seam (ops.link) added since capability errors cannot be provoked on a normal tmpdir. 3. Aged *.steal.* orphans (>10 min; the in-flight window is microseconds) are swept best-effort on steal entry, scoped to the lock's own prefix. Mutation results recorded in the PR body.
admarble
deleted the
feature/908-fix-locks-stale-lock-steal-is-unlink-then-create-n
branch
August 8, 2026 03:47
This was referenced Aug 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Stealing a stale lock was unlink-then-create in both
LockManagerandCheckoutLock— two syscalls with a window in which two sessions that both classified the same lock stale could each destroy the other's freshly created lock and both believe they hold it (the exact two-holder failure the lock exists to prevent, and silent). This routes the steal through one sharedstealStaleLockhelper (alongside the already-sharedclassifyStaleness) that:rename(lockPath → tmp)— of two racing stealers, exactly one moves the current inode; the loser getsENOENT.links it back into place untouched rather than destroying it, and reports the steal lost.Win or lose,
acquirefalls through to the terminalopenSync(…, "wx"), whoseO_CREAT|O_EXCLarbitrates the real holder.The
/specplan (AC-1) prescribed a plain rename-away:rename → unlink tmp → ENOENT = lost, then fall through. That is behaviorally identical to theunlinkit replaces — a barerename(lockPath, …)moves whatever inode is at the path, fresh or not, exactly likeunlink. I verified this with a deterministic hand-driven interleave of both strategies: in the issue's own documented ordering (A.steal → A.create → B.steal → B.create), plain rename-away yields two holders, same as today. It also makes AC-4's mutation test impossible (CAS ≡ unlink ⇒ nothing to mutate).The identity check in step 2/3 is what actually makes AC-1's requirement ("loser cannot remove the winner's fresh lock") hold and makes AC-4 mutation-verifiable. Full analysis in issue comment.
Plain lock files admit no atomic compare-and-swap on content, so one sub-millisecond window remains: a third session's
O_CREAT|O_EXCLcreate racing ourlink-back restore. It is far narrower than the original (which failed on a single race, every time a stealer's removal landed on a fresh lock) and never destroys a live lock (worst case: a harmless*.steal.*orphan, whichlist()ignores). Fully closing it is a larger protocol change (claim file / lease) — recommend a follow-up issue rather than expanding this PR.Pre-PR AC Verification
lockPathdirectly; loser cannot remove the winner's fresh locklock-manager.tsstealStaleLock(rename+verify+link-restore); bothacquirebranches wiredCheckoutLockimportsstealStaleLockfromlock-manager.ts; interleave test passes against bothMutation result (AC-4 / repo gate-test rule)
Mutating
stealStaleLock's identity check to always-match (⇒ blind unlink-then-create):checkout-lock.test.tsandlock-manager.test.ts→ "a loser cannot remove the winner's fresh lock (documented ordering)".Restored; suite green again.
Test plan
npm run build— passesnpm run lint— passes (0 warnings)checkout-lock.test.ts,lock-manager.test.ts) — 89 passedcheckout-lock.integration.test.ts— 7 pre-existing failures (SyntaxError: Unexpected end of JSON input,ExperimentalWarning-polluted subprocess stdout). Confirmed identical at the base commit5a689301in the same worktree → not introduced by this PR (the steal branch doesn't run in those fresh-lock race tests). CI is the backstop.Closes #908
🤖 Generated with Claude Code
QA gap fixes (
5cf65c2f)Fresh-session QA (review) verdicted
AC_MET_BUT_NOT_A_PLUSwith three non-blocking gaps; all three are now filled in this PR:stealStaleLock; nothing provedacquire()uses it. New tests inject theisPidAliveprobe to swap in a fresh lock inside the classification→steal window — a blind-unlink call site destroys it and wrongly wins; the CAS refuses.stealStaleLocknever throws. Rename errors degrade to a lost steal (the terminalO_EXCLcreate surfaces real environment errors with the pre-fix(locks): stale-lock steal is unlink-then-create, not compare-and-swap — two stealers can both win #908 errno); a hard-link capability failure (EPERM/ENOTSUP) now falls back to rename-restore, because leaving the fresh lock renamed-away IS the two-holder bug. Test seam (ops.link) added — capability errors can't be provoked on a normal tmpdir.*.steal.*files (>10 min; the in-flight window is microseconds) are reaped best-effort on steal entry, scoped to the lock's own prefix — recent and foreign-lock orphans untouched.Mutation results for the gap-fix tests (repo gate-test rule)
LockManager.acquirestale branch →unlinkSafe(lockPath)CheckoutLock.acquirestale branch →unlinkSafe(lockPath)sweepStealOrphanscall93/93 lock unit tests green post-restore; worktree clean; typecheck clean.