Repository navigation
Description
Summary
withRegistryLock busy-waits synchronously with Atomics.wait for up to 15 s while another holder has the lock, so the whole server stops answering. Separately, if writing the lock content fails after the lock file is created, the fd is not closed and an empty lock is left that blocks writes for 30 s.
Where
src/utils/fileLock.ts:13-16src/utils/fileLock.ts:33-35src/utils/fileLock.ts:95-147src/utils/sessions.ts:276-289src/utils/sessions.ts:366-375
Details
withRegistryLock is synchronous. Its retry loop sleeps with Atomics.wait (line 34) until deadline = Date.now() + waitMs with LOCK_WAIT_MS = 15_000 (16, 92). The comment at 144-146 says each wait "stays well under 0.1 s", but the loop as a whole can block for 15 s. A lock whose holder pid looks alive (a recycled pid after a crash) counts as stale only after 30 s (13, 27-29). Every persist() runs through this loop, including tryUpdate from child-exit and finalizer handlers (sessions.ts:366-375). Separately, at lines 97-100 a writeFileSync(fd, mine) failure after openSync(lock, "wx") (for example ENOSPC) skips closeSync and leaves an empty lock file. Waiters can't parse it and judge it by mtime alone (119-120), so every registry write fails for 30 s.
Impact
While another instance holds the lock, or a crashed instance left one behind with a recycled pid, the MCP server can't answer any request, process ffmpeg output, or run timers for up to 15 s per write, twice in a row before the lock ages out. With a full disk, the leaked empty lock makes stop/update writes fail for 30 s.
Steps to reproduce
Write sessions.json.lock containing {"pid":<pid of any live process>,"at":<now>} and call any tool that writes the registry. The server stalls for 15 s, then errors. Every other in-flight call stalls with it.
Suggested fix
Make the busy wait async for callers that can await it (persist from tool handlers), or cap the synchronous wait at about 1 s and leave the change pending. In the acquire path, wrap the write in try/finally that closes the fd and unlinks the lock if the write fails.
Acceptance criteria
- The wait is async for callers that can await it, or capped at about 1 s with the change left pending
- A failed lock write closes the fd and removes the lock file
Getting started
- Relevant files:
src/utils/fileLock.ts,src/utils/sessions.ts - Run tests:
npm test(alsonpm run typecheckandnpm run lint); see CONTRIBUTING.md - If you change a tool's schema or description, run
npm run docs
Filed by repo-audit/full-audit at f0e5ebd. Severity: low. Effort: M. audit-key: reliability|src/utils/fileLock.ts|withRegistryLock
Activity