Skip to content

fix(mcpdo): restore a fresh OAuth-flow lock instead of deleting it on a lost steal - #2696

Merged
cliffhall merged 1 commit into
v2/mainfrom
v2/fix/2643-stale-lock-steal-restore
Oct 12, 2026
Merged

cliffhall merged 1 commit into
v2/mainfrom
v2/fix/2643-stale-lock-steal-restore

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #2643

Problem

In tryReserveAuthFlow (clients/mcpdo/src/connection/auth-helper.ts), when two stealers race on a stale per-server lock, stealer B can rename stealer A's fresh lock to its claim path. B correctly detects the claim is fresh and backs off — but it did so with fs.rmSync(claimPath), deleting A's lock. A third connect then wins create() and spawns a second sign-in helper, and the two contend for the OAuth callback port.

Fix

On the fresh-claim branch, put the lock back with fs.linkSync(claimPath, lockPath) before removing the claim. The hard link keeps the same inode (so A's mtime and contents are preserved), and it fails with EEXIST if a newer lock already took the path — in which case the outer catch backs off as before. Only a lock confirmed stale is ever discarded. The v8 ignore on that branch is gone: it's now covered.

Tests

auth-helper.test.ts gains a pass-through renameSync seam (the same pattern daemon-coverage.test.ts uses) and two cases that inject the interleaving inside the stat→rename window:

  • restores the fresh lock instead of deleting it — fails on the old code (lock gone), passes now.
  • backs off without restoring when a newer lock already took the path — the link refuses to clobber the third reserver's lock and the claim file is cleaned up.

npm run local:gate passes.

🤖 Generated with Claude Code

… a lost steal (#2643)

When two stealers interleave on a stale per-server lock, the second one
renames the first's fresh lock, sees it is fresh and backs off - but
deleted it on the way out, letting a third connect win create() and spawn
a competing sign-in helper. Link the claim back to the lock path (which
refuses if a newer lock exists) before removing the claim, so only a lock
confirmed stale is ever discarded. Tests inject the interleaving through
a pass-through renameSync seam.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: cliffhall <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused implementation matches the issue requirements and covers both relevant race outcomes.

0 open findings

What changed in this PR

Fixes #2643 by preserving a fresh OAuth-flow lock encountered during a stale-lock steal race.

Changes:

  • Restores fresh locks using a create-only hard link.
  • Adds targeted coverage for restoration and EEXIST races.
File Description
clients/​mcpdo/​src/​connection/​auth-helper.ts Restores fresh claimed locks before backing off.
clients/​mcpdo/​__tests__/​auth-helper.test.ts Tests both lock-restoration outcomes.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot review loop closed: round 1 was clean (0 inline comments, nothing suppressed, approval recommended), so no further rounds were requested.

@cliffhall
cliffhall merged commit f81c2e4 into v2/main Oct 12, 2026
8 checks passed
@cliffhall
cliffhall deleted the v2/fix/2643-stale-lock-steal-restore branch October 12, 2026 19:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Issues and PRs for v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mcpdo: a stale OAuth-flow lock steal can delete a fresh lock and allow a second sign-in helper

2 participants