Skip to content

Commit a3bb558

Browse files
committed
fix(browser): recover abandoned install locks
1 parent 758a6d2 commit a3bb558

2 files changed

Lines changed: 50 additions & 4 deletions

File tree

‎packages/cli/src/browser/manager.test.ts‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,14 @@ interface FsMockOptions {
6262
/** map of dir path -> entries returned by readdirSync */
6363
dirs?: Record<string, string[]>;
6464
touchError?: Error;
65+
initialMtimeMs?: number;
6566
}
6667

67-
function installFsMocks({ existing, dirs, touchError }: FsMockOptions) {
68+
function installFsMocks({ existing, dirs, touchError, initialMtimeMs = 0 }: FsMockOptions) {
6869
// Mutable, and returned, so tests can pre-seed a "lock already held" path or
6970
// assert the lock dir doesn't leak after ensureBrowser resolves.
7071
const paths = new Set(existing);
71-
const mtimes = new Map([...existing].map((p) => [p, 0]));
72+
const mtimes = new Map([...existing].map((p) => [p, initialMtimeMs]));
7273
vi.doMock("node:fs", () => ({
7374
existsSync: (p: string) => paths.has(p),
7475
readdirSync: (p: string) => {
@@ -174,6 +175,7 @@ describe("findBrowser — cache resolution", () => {
174175
});
175176

176177
afterEach(() => {
178+
vi.useRealTimers();
177179
Object.defineProperty(process, "platform", { value: origPlatform, configurable: true });
178180
Object.defineProperty(process, "arch", { value: origArch, configurable: true });
179181
vi.restoreAllMocks();
@@ -347,6 +349,23 @@ describe("findBrowser — cache resolution", () => {
347349
expect(paths.has(HF_LOCK)).toBe(false);
348350
});
349351

352+
it("withInstallLock recovers when a crashed reclaimer leaves both lock directories", async () => {
353+
vi.useFakeTimers();
354+
const paths = installFsMocks({
355+
existing: new Set([CACHE_ROOT, HF_LOCK, HF_RECLAIM_LOCK]),
356+
});
357+
358+
const { withInstallLock } = await import("./manager.js");
359+
const acquisition = withInstallLock(async () => "done", TEST_LOCK_TIMINGS);
360+
await vi.advanceTimersByTimeAsync(TEST_LOCK_TIMINGS.pollMs * 2);
361+
362+
await expect(Promise.race([acquisition, Promise.resolve("still waiting")])).resolves.toBe(
363+
"done",
364+
);
365+
expect(paths.has(HF_LOCK)).toBe(false);
366+
expect(paths.has(HF_RECLAIM_LOCK)).toBe(false);
367+
});
368+
350369
it("withInstallLock does not reclaim another waiter's fresh lock after this waiter timed out", async () => {
351370
// Regression guard for the timeout-reclaim race: if multiple waiters cross
352371
// the stale-lock deadline together, waiter A can reclaim the stale lock and
@@ -392,7 +411,10 @@ describe("findBrowser — cache resolution", () => {
392411
});
393412

394413
it("withInstallLock reports progress while waiting instead of staying silent", async () => {
395-
const paths = installFsMocks({ existing: new Set([CACHE_ROOT, HF_LOCK]) });
414+
const paths = installFsMocks({
415+
existing: new Set([CACHE_ROOT, HF_LOCK]),
416+
initialMtimeMs: Date.now(),
417+
});
396418
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
397419

398420
const { withInstallLock } = await import("./manager.js");

‎packages/cli/src/browser/manager.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ function tryAcquireDirLock(lockDir: string): boolean {
7777
}
7878
}
7979

80+
function isDirLockStale(lockDir: string, timeoutMs: number): boolean {
81+
try {
82+
return Date.now() - statSync(lockDir).mtimeMs > timeoutMs;
83+
} catch (err) {
84+
if (isErrno(err, "ENOENT")) return false;
85+
throw err;
86+
}
87+
}
88+
8089
function reclaimStaleInstallLock(timeoutMs: number): void {
8190
if (!tryAcquireDirLock(INSTALL_RECLAIM_LOCK_DIR)) return;
8291
try {
@@ -91,6 +100,16 @@ function reclaimStaleInstallLock(timeoutMs: number): void {
91100
}
92101
}
93102

103+
function reclaimAbandonedReclaimLock(timeoutMs: number): void {
104+
try {
105+
if (isDirLockStale(INSTALL_RECLAIM_LOCK_DIR, timeoutMs)) {
106+
rmSync(INSTALL_RECLAIM_LOCK_DIR, { recursive: true, force: true });
107+
}
108+
} catch (err) {
109+
if (!isErrno(err, "ENOENT")) throw err;
110+
}
111+
}
112+
94113
function touchInstallLock(): void {
95114
try {
96115
const now = new Date();
@@ -113,6 +132,11 @@ export async function withInstallLock<T>(
113132
let lastNoticeMs = 0;
114133
for (;;) {
115134
if (existsSync(INSTALL_RECLAIM_LOCK_DIR)) {
135+
// A process can die after acquiring the reclaim gate but before its
136+
// synchronous cleanup runs. Without aging out that gate, every future
137+
// installer sleeps here forever and never reaches the install-lock
138+
// timeout/reclaim path below.
139+
reclaimAbandonedReclaimLock(timings.staleMs);
116140
await sleep(timings.pollMs);
117141
continue;
118142
}
@@ -127,7 +151,7 @@ export async function withInstallLock<T>(
127151
`[browser] Waiting for another hyperframes process to finish installing chrome-headless-shell (${Math.round(waitedMs / 1000)}s elapsed)...`,
128152
);
129153
}
130-
if (Date.now() > deadline) {
154+
if (isDirLockStale(INSTALL_LOCK_DIR, timings.staleMs) || Date.now() > deadline) {
131155
// The reclaim gate matters when multiple waiters cross the timeout at
132156
// once: without it, waiter A can delete the stale lock and acquire a
133157
// fresh one, then waiter B (whose old deadline also expired) can delete

0 commit comments

Comments
 (0)