Skip to content

fix(memory): keep retrying a lock put-back that Windows refuses for a moment - #5637

Merged
cliffhall merged 2 commits into
v2/mainfrom
v2/fix/5635-memory-lock-handoff-windows
Nov 19, 2026
Merged

cliffhall merged 2 commits into
v2/mainfrom
v2/fix/5635-memory-lock-handoff-windows

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #5635

Description

On Windows, the memory server's lock could still let two writers run at once. This is the hand-off #5565 meant to close (#5623). When a stale-lock break takes a successor's live lock aside by mistake, it renames that lock back over whatever a third writer created at the lock path in the gap. On Windows, a rename that replaces a file fails with EPERM while anything holds that file open. The third writer keeps opening its own lock file while it waits out the break (confirmLock → refreshLock), so the put-back could fail.

The failure was then swallowed as a busy lock, and the guard was dropped with the live lock still aside. The third writer confirmed its own token and ran beside the successor's holder.

The put-back now goes through restoreLock, which retries under the guard while Windows refuses the rename. It retries only on EPERM, EACCES or EBUSY, and only on win32. The retries stop after staleMs, past which the guard is taken to be a crash's and protects nothing anyway. Off Windows those codes mean a lasting failure, which still propagates at once.

Server Details

  • Server: memory
  • Changes to: the cross-process lock's stale-break path (internal), tests, and a patch changeset. No tool, resource, prompt, schema or configuration changes, so the README is unchanged.

Motivation and Context

#5635: since #5623 merged, Test memory (Windows) had failed 4 of its first 9 runs, every PR included, on "does not let a writer that created the lock while a live one was aside run beside its holder". The cause was established on Windows CI with a throwaway branch that ran that test 20 times with the renames instrumented:

Windows, 20 runs of the unchanged test Failed Restore rename failed with EPERM Third writer ran Live lock stranded aside
v2/main's index.ts (job 135662372066) 8 8 8 8
This branch's index.ts (job 135663708895) 0 — 0 0

So this was a defect in the lock code on Windows, not a timing assumption in the test.

How Has This Been Tested?

  • Three new deterministic tests in file-lock.test.ts, each injecting the failure at the restore rename with process.platform mocked:
    • On win32, two EPERM refusals are retried: the breaker and the third writer both time out, the third never runs, and the successor's lock is back in place with nothing left over.
    • Off Windows, EPERM is not retried and propagates.
    • On win32, a refusal that outlasts staleMs stops the retries and propagates.
  • Against v2/main's index.ts, the first and third fail. The first fails exactly as memory: hand-off test "does not let a writer that created the lock while a live one was aside run beside its holder" fails intermittently on Windows since #5623 #5635 does, with the breaker resolving instead of timing out.
  • On this branch, all 57 tests in file-lock.test.ts pass.
  • Windows CI: 20 of 20 runs of the original hand-off test pass (the table above).
  • npm run local:gate exits 0.
  • LLM client: not applicable. The change is internal to the lock taken between processes and is not visible over MCP, and Windows-only file semantics can't be exercised from a client on macOS. Every write tool goes through this lock, and the memory suite's protocol-level tests pass.

Breaking Changes

None.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I have read the MCP Protocol Documentation: not applicable beyond the server's own behavior; no protocol surface changes
  • My changes follow MCP security best practices
  • I have updated the server's README accordingly: no user-facing change to document
  • I have tested this with an LLM client: not applicable, as above
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have documented all environment variables and configuration options: none added

🤖 Generated with Claude Code

… moment

On Windows a rename over a file that anything holds open fails with EPERM.
When a stale-lock break took a live lock aside by mistake, the writer that
created the lock in the gap kept opening its file while it waited out the
break, so putting the live lock back could fail. The failure was swallowed
as a busy lock, the guard was dropped with the live lock still aside, and
that writer ran beside its holder: the #5565 hand-off window, reopened on
Windows. The put-back is now retried under the guard until it lands, for
up to staleMs.

Closes #5635

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
@cliffhall cliffhall added the v2 label Nov 19, 2026
@changeset-bot

changeset-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7a1511c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@modelcontextprotocol/server-memory Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

memory: MCP interface diff

Base: 640a8c31779b (Merge pull request #5614 from modelcontextprotocol/v2/chore/5527-mcp-interface-diff-rename)
Head: the build in this checkout, at cb7db316cd9b (Merge 7a1511c into 640a8c3)

Diffed because it was named (--server).

✅ No interface changes detected.

Tools Prompts Resources Resource templates
Base 9 0 1 0
Head 9 0 1 0

Updated by this run for 7a1511cd2e0e6ce1019e259bdef959621492fe26.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Critical lock-restoration flaws can allow concurrent writes after guard expiry and misclassify exhausted EPERM failures as contention, resuming acquisition with the live lock still set aside.

2 open issues
What changed in this PR

This PR addresses #5635 by adding retries for Windows rename failures when the memory server restores a live lock during stale-lock cleanup.

Changes:

  • Retries Windows EPERM, EACCES, and EBUSY restoration failures under the breaking guard.
  • Adds tests for transient failures, non-Windows error propagation, and retry exhaustion.
  • Includes a patch changeset; no public MCP interface or configuration changes.
File Description
src/​memory/​index.ts Adds guarded restoration retries and passes through retry timing.
src/​memory/​__tests__/​file-lock.test.ts Adds three restoration-failure regression tests.
.changeset/​memory-lock-restore-windows.md Records the memory server patch release note.

Review effort: Balanced

Comment thread src/memory/index.ts Resolved
Comment thread src/memory/index.ts Resolved
…inly

Review of #5637. The retry deadline counted from the first attempt rather
than from when the guard was taken, and was not re-checked after the pause,
so a retry could land after the guard went stale and another writer moved
on. It now counts from just before the guard is taken, at half of staleMs,
and is checked before every retry. A put-back that never lands now fails
with an error of its own (the rename's error as its cause): Windows' EPERM
would otherwise read as a busy lock and send the breaker back to acquiring
with the live lock still aside.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
@cliffhall

Copy link
Copy Markdown
Member Author

Round 1 answered in 7a1511c (gate exit 0). Both findings were defects in code this PR added, and both are fixed:

  • The retry deadline now counts from just before the guard is taken, at half of staleMs, and is checked before every retry.
  • A put-back that runs out now fails with an error of its own, with the rename's error as its cause, so Windows' EPERM is no longer read as a busy lock.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the Windows lock-restoration fix.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

2 participants