Repository navigation
Description
Problem
The memory server's cross-process lock uses a second exclusive-create file, the guard (<memory file>.lock.break), to order stale-lock breaking and, since #5565 (PR #5623), every release and every newly created lock's confirmation. A guard left behind by a process that crashed while holding it is cleared by whoever finds it older than the stale timeout (30 seconds by default).
That clearing is done by path: the finder opens the guard, judges its age through the handle, and then calls unlink on the guard's path (isGuardHeld in src/memory/index.ts; before #5565 the same stat then unlink in breakStaleLock). Two processes clearing the same crash-left guard at once can therefore remove a live one:
- A and B both open the crash-left guard and judge it stale.
- A unlinks it.
- C (a releasing holder, or a breaker) creates a fresh guard at the same path.
- B unlinks the path, deleting C's fresh guard.
C then runs its few guarded file-system calls unguarded, so a breaker can act between a release's token comparison and its removal (the third window of #5565), or two breakers can run at once.
It takes a crash inside a guarded section (a few file-system calls per write), then two processes clearing that guard within the same moment, then a third taking the guard between their two unlinks. The processes run as the same user, so this is a correctness gap, not a security boundary. Raised by Copilot in review of #5623 and declined there as beyond the three windows #5565 names.
Expected behavior
Clearing a guard left by a crash never removes a guard another process has taken since.
Approach
To be decided. Renaming the guard aside and comparing device and inode with the judged file before deleting it, as breakStaleLock does for the lock itself (#5560), removes exactly this interleaving, but putting a mistakenly taken live guard back has its own gap one level down (a create at the guard path while it is aside). Whatever is chosen should say what remains, as the comment above guardPathOf() does today.
Acceptance criteria
- A deterministic test (no timing dependence): two clearers of one crash-left guard, with a fresh guard created between their removals; the fresh guard survives.
- The existing lock tests still pass.
Activity
A7mdMe commentedon Nov 19, 2026
Sharp observation on this ABA race window, @cliffhall.
The core vulnerability stems from
unlink(guardPath)operating by pathname rather than on the file descriptor/inode identity that was evaluated stale.One clean, robust way to eliminate this interleaving without the secondary gap of putting an aside file back is the Atomic Rename-Aside Staging Pattern:
Architectural Mechanism
Rather than having a clearing process call
unlink(guardPath)directly:fs.openSync), evaluates its age viafstatSync, and capturesstat.ino/stat.dev.fs.statSync(stagingPath).ino === statBefore.ino).unlinkSync(stagingPath).Why this completely closes the ABA window:
guardPath.ENOENTfromrenameSyncand safely aborts.guardPath, Process B is never in a position to blindly unlinkguardPath. Even if Process B retried,clearStaleGuardSafelywould inspect C's fresh guard, seeage < staleTimeout, and immediately abort withNOT_STALE.Deterministic Concurrency Test Pattern (No Timing Dependence)
(Disclosure: I maintain A7mdMe/mcp-production-kit, where we test and harden production MCP client/server boundary and concurrency defenses).
anassk01 commentedon Nov 23, 2026
One thing to watch on issue 5624 is inode reuse on Linux tmpfs and ext4. After Process A unlinks .lock.break and Process C creates a fresh guard, the kernel can reuse the same inode number, so checking dev and ino alone still lets Process B delete C's live guard. Taking .lock.break.reclaim first and checking an owner token inside the guard keeps C's guard intact, and I wrote a zero-timer node:test for it if helpful.