Skip to content

Commit d3aaa38

Browse files
cliffhallclaude
andcommitted
fix(memory): give the lock guard up in one synchronous step
Review of #5623: dropGuard awaited the guard's unlink and only then stopped tracking it. The unlink takes effect before its continuation runs, so another process could take the guard in between, and an exit arriving then found the path still tracked and removed that process's guard as its own. The removal and the end of tracking are now synchronous, which leaves no turn of the event loop between them. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 2d3e85c commit d3aaa38

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,29 @@ describe("withFileLock", () => {
814814
},
815815
);
816816

817+
it("gives the guard up in one synchronous step, so an exit cannot find it tracked but gone", async () => {
818+
// Were the guard removed by an awaited unlink, an exit between the
819+
// removal and its continuation would treat a guard another process
820+
// had taken since as this process's own, and remove it.
821+
const unlink = vi.spyOn(fs, "unlink");
822+
let guardSeen = false;
823+
const realReadFile = fs.readFile;
824+
await withFileLock(lockPath, async () => {
825+
vi.spyOn(fs, "readFile").mockImplementationOnce((async (
826+
...args: Parameters<typeof fs.readFile>
827+
) => {
828+
guardSeen = await exists(guardPath());
829+
return realReadFile(...args);
830+
}) as typeof fs.readFile);
831+
});
832+
833+
expect(guardSeen).toBe(true);
834+
expect(unlink.mock.calls.map(([file]) => String(file))).not.toContain(
835+
guardPath(),
836+
);
837+
expect(await fs.readdir(dir)).toEqual([]);
838+
});
839+
817840
it("waits for the guard before releasing", async () => {
818841
const opens = watchOpens();
819842
let contended: Promise<void> | undefined;

‎src/memory/index.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,17 @@ async function tryTakeGuard(
431431
return true;
432432
}
433433

434-
// Give up a guard this process took.
435-
async function dropGuard(guardPath: string): Promise<void> {
436-
await fs.unlink(guardPath).catch(ignoreError);
434+
// Give up a guard this process took. The removal and the end of its tracking
435+
// are one synchronous step: were the removal awaited, an exit arriving before
436+
// its continuation would still find the guard tracked, and the exit hook
437+
// would remove, as this process's own, a guard another process had taken at
438+
// the same path in between.
439+
function dropGuard(guardPath: string): void {
440+
try {
441+
unlinkSync(guardPath);
442+
} catch {
443+
// Already gone: cleared as stale, or removed by the exit hook.
444+
}
437445
heldGuards.delete(guardPath);
438446
}
439447

@@ -492,7 +500,7 @@ async function breakStaleLock(
492500
await judged.close();
493501
}
494502
} finally {
495-
await dropGuard(guardPath);
503+
dropGuard(guardPath);
496504
}
497505
}
498506

@@ -634,7 +642,7 @@ async function releaseLock(
634642
await fs.unlink(lockPath).catch(ignoreError);
635643
}
636644
if (guarded) {
637-
await dropGuard(guardPath);
645+
dropGuard(guardPath);
638646
}
639647
}
640648

0 commit comments

Comments
 (0)