Skip to content

Commit fc7a9be

Browse files
cliffhallclaude
andcommitted
fix(memory): refresh a held lock and remove one that failed to initialize
A holder now refreshes the lock's mtime every staleMs / 2, so a mutation that outlasts staleMs is not broken as abandoned while it runs. A lock whose token could not be written or closed is removed before the error propagates, instead of blocking other writers until it goes stale. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 43b8a1c commit fc7a9be

3 files changed

Lines changed: 71 additions & 10 deletions

File tree

‎src/memory/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ On Windows, use:
236236

237237
Each MCP client starts its own server process, so two clients using the same `MEMORY_FILE_PATH` (or both using the default) are two processes writing one file. Every write tool takes an exclusive lock file, `<memory file>.lock`, next to the memory file while it reads, changes and saves the graph, so writes from different processes take turns and none is lost. The lock file exists only while a write is in progress, so the directory holding the memory file must be writable (it already must be, for the atomic save).
238238

239-
- A lock left behind by a server that crashed mid-write is removed automatically: at once if its process is known to have exited on the same machine, otherwise once it is 30 seconds old.
239+
- A lock left behind by a server that crashed mid-write is removed automatically: at once if its process is known to have exited on the same machine, otherwise once it has gone 30 seconds without being refreshed (a server holding the lock refreshes it every 15 seconds while it works).
240240
- A write that cannot get the lock within 60 seconds fails with a tool error rather than overwriting the file.
241241
- The lock relies on exclusive file creation, which local filesystems provide; a network filesystem that does not honour it cannot be shared safely this way.
242242

‎src/memory/__tests__/file-lock.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,48 @@ describe("withFileLock", () => {
165165
expect(await withFileLock(lockPath, async () => "ran", fast)).toBe("ran");
166166
});
167167

168+
it("keeps a lock held longer than staleMs fresh, so it is not broken", async () => {
169+
const opts = { staleMs: 100, timeoutMs: 5_000, retryMs: 5 };
170+
const order: string[] = [];
171+
const long = withFileLock(
172+
lockPath,
173+
async () => {
174+
order.push("long start");
175+
await new Promise((resolve) => setTimeout(resolve, 400));
176+
order.push("long end");
177+
},
178+
opts,
179+
);
180+
// Let the long holder take the lock before the waiter starts.
181+
await new Promise((resolve) => setTimeout(resolve, 20));
182+
const waiter = withFileLock(
183+
lockPath,
184+
async () => {
185+
order.push("waiter");
186+
},
187+
opts,
188+
);
189+
190+
await Promise.all([long, waiter]);
191+
expect(order).toEqual(["long start", "long end", "waiter"]);
192+
});
193+
194+
it("removes the lock it created when writing it fails", async () => {
195+
const realOpen = fs.open;
196+
vi.spyOn(fs, "open").mockImplementationOnce(async (...args) => {
197+
const handle = await realOpen(...args);
198+
handle.writeFile = async () => {
199+
throw new Error("EIO: simulated write failure");
200+
};
201+
return handle;
202+
});
203+
204+
await expect(withFileLock(lockPath, async () => "ran")).rejects.toThrow(
205+
"EIO: simulated write failure",
206+
);
207+
expect(await exists(lockPath)).toBe(false);
208+
});
209+
168210
it("leaves a lock it no longer owns in place on release", async () => {
169211
await withFileLock(lockPath, async () => {
170212
await fs.writeFile(lockPath, "someone else");

‎src/memory/index.ts‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from "@modelcontextprotocol/sdk/types.js";
1616
import { z } from "zod";
1717
import { promises as fs, realpathSync } from "fs";
18+
import type { FileHandle } from "fs/promises";
1819
import path from "path";
1920
import os from "os";
2021
import { randomBytes } from "crypto";
@@ -105,7 +106,7 @@ export interface KnowledgeGraph {
105106
// Timings for the cross-process lock around each mutation. Exposed so tests
106107
// can shorten them; the server always uses the defaults.
107108
export interface FileLockOptions {
108-
// A lock older than this is presumed abandoned and is broken.
109+
// A lock not refreshed for this long is presumed abandoned and is broken.
109110
staleMs?: number;
110111
// Give up, failing the mutation, after waiting this long for the lock.
111112
timeoutMs?: number;
@@ -140,8 +141,9 @@ function isLockOwnerDead(contents: string): boolean {
140141
// mutations from separate server processes sharing one graph file take turns
141142
// and each one loads the graph the previous one saved (#4797). The lock is a
142143
// file created with O_EXCL, which is atomic on local filesystems: exactly one
143-
// creator wins. A lock left behind by a crashed process is broken once its
144-
// owner is known to be dead, or once it is older than staleMs.
144+
// creator wins. The holder refreshes the lock's mtime every staleMs / 2 while
145+
// it runs. A lock left behind by a crashed process is broken once its owner
146+
// is known to be dead, or once it has gone unrefreshed for staleMs.
145147
export async function withFileLock<T>(
146148
lockPath: string,
147149
operation: () => Promise<T>,
@@ -155,18 +157,26 @@ export async function withFileLock<T>(
155157
const deadline = Date.now() + timeoutMs;
156158

157159
for (;;) {
160+
let handle: FileHandle | undefined;
158161
try {
159-
const handle = await fs.open(lockPath, "wx");
162+
handle = await fs.open(lockPath, "wx");
163+
} catch (error) {
164+
if ((error as NodeJS.ErrnoException).code !== "EEXIST") {
165+
throw error;
166+
}
167+
}
168+
if (handle) {
160169
try {
161170
await handle.writeFile(token);
162-
} finally {
163171
await handle.close();
164-
}
165-
break;
166-
} catch (error) {
167-
if ((error as NodeJS.ErrnoException).code !== "EEXIST") {
172+
} catch (error) {
173+
// The lock was created but not written: remove it rather than leave
174+
// every other writer waiting for it to go stale.
175+
await handle.close().catch(() => {});
176+
await fs.unlink(lockPath).catch(() => {});
168177
throw error;
169178
}
179+
break;
170180
}
171181

172182
// Someone else holds the lock. Break it if it is abandoned; otherwise
@@ -203,9 +213,18 @@ export async function withFileLock<T>(
203213
);
204214
}
205215

216+
// Keep the lock fresh while the operation runs, so a mutation that outlasts
217+
// staleMs (a large graph, a slow disk) is not mistaken for an abandoned one.
218+
const heartbeat = setInterval(() => {
219+
const now = new Date();
220+
fs.utimes(lockPath, now, now).catch(() => {});
221+
}, staleMs / 2);
222+
heartbeat.unref();
223+
206224
try {
207225
return await operation();
208226
} finally {
227+
clearInterval(heartbeat);
209228
// Release only our own lock: if it was broken as stale while we held it,
210229
// the file may now belong to another process.
211230
const current = await fs.readFile(lockPath, "utf-8").catch(() => null);

0 commit comments

Comments
 (0)