Skip to content

Commit 4efde40

Browse files
cliffhallclaude
andcommitted
fix(memory): match a written lock's token exactly at exit
Copilot round 2 on #5530: once our token is written, a prefix match also matched another writer's empty replacement lock, letting exit cleanup remove it. Record whether the token write finished; only an unwritten lock, which cannot yet have been judged stale and replaced, takes the prefix match. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 72d35db commit 4efde40

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,19 @@ describe("releasing held locks on exit (#5485)", () => {
489489
expect(await held).toBe("ran");
490490
});
491491

492+
it("leaves another writer's empty replacement of a written lock in place", async () => {
493+
const lockPath = path.join(dir, "memory.jsonl.lock");
494+
await withFileLock(lockPath, async () => {
495+
// Our lock was broken as stale, and a new writer has created its own
496+
// but not yet written its token.
497+
await fs.writeFile(lockPath, "");
498+
499+
releaseHeldLocks();
500+
501+
expect(await exists(lockPath)).toBe(true);
502+
});
503+
});
504+
492505
it("still tracks a lock while its release is in progress", async () => {
493506
const lockPath = path.join(dir, "memory.jsonl.lock");
494507
const realUnlink = fs.unlink;

‎src/memory/index.ts‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,17 @@ async function breakStaleLock(
277277
}
278278

279279
// The lock files this process holds right now, each mapped to the token it
280-
// wrote into it. withFileLock releases its lock in a `finally`, which never
280+
// writes into it and whether that write has finished. withFileLock releases its lock in a `finally`, which never
281281
// runs when a signal ends the process, so the exit hook releaseHeldLocks()
282282
// removes them instead (#5485). Without it, a lock left by a stopped server
283283
// stalls the next writer until it goes stale, which takes the full staleMs
284284
// when that writer is on another host (another container) and cannot see
285285
// that this process has gone.
286-
const heldLocks = new Map<string, string>();
286+
interface HeldLock {
287+
token: string;
288+
written: boolean;
289+
}
290+
const heldLocks = new Map<string, HeldLock>();
287291

288292
// The lock paths this process holds right now, for tests.
289293
export function heldLockPaths(): string[] {
@@ -295,12 +299,17 @@ export function heldLockPaths(): string[] {
295299
// release, a lock is removed only while it still holds this process's token,
296300
// so a lock broken as stale and re-taken by another process stays in place.
297301
// A lock is tracked from the moment its exclusive create returns, before its
298-
// token is written, so a file holding only the start of the token (or none of
299-
// it) is ours too: the exit interrupted the write.
302+
// token is written. Until that write finishes, a file holding only the start
303+
// of the token (or none of it) is ours too: the exit interrupted the write.
304+
// Nothing else can have replaced it by then, since a lock that new, with a
305+
// live owner, is never judged stale. Once the token is written only an exact
306+
// match is ours, so another writer's replacement, empty while its own token
307+
// is being written, is left alone.
300308
export function releaseHeldLocks(): void {
301-
for (const [lockPath, token] of heldLocks) {
309+
for (const [lockPath, { token, written }] of heldLocks) {
302310
try {
303-
if (token.startsWith(readFileSync(lockPath, "utf-8"))) {
311+
const contents = readFileSync(lockPath, "utf-8");
312+
if (written ? contents === token : token.startsWith(contents)) {
304313
unlinkSync(lockPath);
305314
}
306315
} catch {
@@ -365,9 +374,11 @@ export async function withFileLock<T>(
365374
// Track the lock before anything else is awaited: a signal is handled
366375
// only between tasks, never between the open's completion and this
367376
// line, so from here on the exit hook can remove it (#5485).
368-
heldLocks.set(lockPath, token);
377+
const held: HeldLock = { token, written: false };
378+
heldLocks.set(lockPath, held);
369379
try {
370380
await handle.writeFile(token);
381+
held.written = true;
371382
await handle.close();
372383
} catch (error) {
373384
// The lock was created but not written: remove it rather than leave
@@ -447,7 +458,7 @@ export async function withFileLock<T>(
447458
// it still removes the lock. A lock this same process broke as stale and
448459
// re-took in another withFileLock call carries that call's token, and
449460
// stays tracked for it.
450-
if (heldLocks.get(lockPath) === token) {
461+
if (heldLocks.get(lockPath)?.token === token) {
451462
heldLocks.delete(lockPath);
452463
}
453464
}

0 commit comments

Comments
 (0)