Skip to content

Commit 7d1fcf1

Browse files
authored
Merge pull request #5462 from modelcontextprotocol/v2/fix/5418-memory-heartbeat-ownership
fix(memory): stop the lock heartbeat refreshing a lock it no longer owns
2 parents 9937c7c + 698222f commit 7d1fcf1

3 files changed

Lines changed: 67 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-memory": patch
3+
---
4+
5+
Fix the file lock's heartbeat refreshing a lock file this process no longer owns: once a lock is broken as stale and re-taken by another process, the previous holder stops refreshing it, so a crashed new owner's lock can still go stale and be broken.

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,52 @@ describe("withFileLock", () => {
250250
expect(order).toEqual(["long start", "long end", "waiter"]);
251251
});
252252

253+
it("stops refreshing a lock that was broken and re-taken while it ran (#5418)", async () => {
254+
// Heartbeat every 100 ms, wide enough that the replacement below finishes
255+
// before the next beat; the operation outlives several beats.
256+
const opts = { staleMs: 200, timeoutMs: 5_000, retryMs: 5 };
257+
const old = new Date(Date.now() - 60_000);
258+
const realUtimes = fs.utimes;
259+
let replaced!: () => void;
260+
const replacement = new Promise<void>((resolve) => (replaced = resolve));
261+
// Our first heartbeat refreshes our own lock; right after it, another
262+
// process breaks the lock as stale and re-takes it with its own token.
263+
const utimes = vi
264+
.spyOn(fs, "utimes")
265+
.mockImplementationOnce(async (...args) => {
266+
await realUtimes(...args);
267+
await fs.writeFile(lockPath, "someone else");
268+
await realUtimes(lockPath, old, old);
269+
replaced();
270+
});
271+
const readFile = vi.spyOn(fs, "readFile");
272+
const lockReads = () =>
273+
readFile.mock.calls.filter(([file]) => file === lockPath).length;
274+
275+
await withFileLock(
276+
lockPath,
277+
async () => {
278+
await replacement;
279+
readFile.mockClear();
280+
// Several heartbeats later, only the first one, on our own lock, has
281+
// touched the file: the next one read the new owner's token and stopped
282+
// the interval, so the beats after it do not even read the file.
283+
await new Promise((resolve) => setTimeout(resolve, 250));
284+
expect(utimes).toHaveBeenCalledTimes(1);
285+
const reads = lockReads();
286+
expect(reads).toBeGreaterThan(0);
287+
await new Promise((resolve) => setTimeout(resolve, 350));
288+
expect(lockReads()).toBe(reads);
289+
},
290+
opts,
291+
);
292+
293+
expect(utimes).toHaveBeenCalledTimes(1);
294+
expect(await fs.readFile(lockPath, "utf-8")).toBe("someone else");
295+
const { mtimeMs } = await fs.stat(lockPath);
296+
expect(mtimeMs).toBeLessThan(Date.now() - 30_000);
297+
});
298+
253299
it("removes the lock it created when writing it fails", async () => {
254300
const realOpen = fs.open;
255301
vi.spyOn(fs, "open").mockImplementationOnce(async (...args) => {

‎src/memory/index.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,24 @@ export async function withFileLock<T>(
350350

351351
// Keep the lock fresh while the operation runs, so a mutation that outlasts
352352
// staleMs (a large graph, a slow disk) is not mistaken for an abandoned one.
353+
// Refresh only our own lock, as the release below does: once it is broken
354+
// as stale and re-taken, the file belongs to another process, and keeping
355+
// it fresh would stop a crashed new owner's lock from ever going stale.
353356
const heartbeat = setInterval(() => {
354357
const now = new Date();
355-
fs.utimes(lockPath, now, now).catch(ignoreError);
358+
// Not awaited: an interval callback has no caller to await it, and every
359+
// failure (the lock already gone) is caught and ignored here.
360+
void fs
361+
.readFile(lockPath, "utf-8")
362+
.then((current) => {
363+
if (current !== token) {
364+
// The lock changed hands: stop refreshing it, for good.
365+
clearInterval(heartbeat);
366+
return;
367+
}
368+
return fs.utimes(lockPath, now, now);
369+
})
370+
.catch(ignoreError);
356371
}, staleMs / 2);
357372
heartbeat.unref();
358373

0 commit comments

Comments
 (0)