Skip to content

Commit 6874cc2

Browse files
fix(cli): a locked Chrome debug.log on Windows no longer fails check, snapshot or render (#6771)
1 parent 9c01e2e commit 6874cc2

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,38 @@ describe("findBrowser — cache resolution", () => {
739739
}
740740
});
741741

742+
it("does not fail a run when Windows holds a leftover's debug.log", async () => {
743+
const home = useRealCacheHome();
744+
const root = join(home, ".cache", "hyperframes");
745+
const held = join(root, ".chrome-replaced-dead");
746+
vi.doMock("node:fs", async (importOriginal) => {
747+
const actual = await importOriginal<typeof import("node:fs")>();
748+
return {
749+
...actual,
750+
rmSync: (path: string, options?: Parameters<typeof actual.rmSync>[1]) => {
751+
if (!String(path).includes(".chrome-replaced-")) return actual.rmSync(path, options);
752+
throw Object.assign(
753+
new Error(`EBUSY: resource busy or locked, unlink '${path}/debug.log'`),
754+
{ code: "EBUSY" },
755+
);
756+
},
757+
};
758+
});
759+
installPuppeteerBrowsersMock({
760+
installImpl: async ({ cacheDir, buildId }) => writeStagedInstall(cacheDir, buildId, "fresh"),
761+
});
762+
try {
763+
const { ensureBrowser, sweepKilledInstalls } = await import("./manager.js");
764+
mkdirSync(held, { recursive: true });
765+
await expect(sweepKilledInstalls()).resolves.toBeUndefined();
766+
await expect(ensureBrowser({ force: true })).resolves.toMatchObject({ source: "download" });
767+
await ensureBrowser({ force: true });
768+
expect(existsSync(held)).toBe(true);
769+
} finally {
770+
rmSync(home, { recursive: true, force: true });
771+
}
772+
});
773+
742774
it("serializes concurrent force downloads so they never install at the same time", async () => {
743775
const downloadedBinary = join(HF_CACHE, "chrome-headless-shell", "force-downloaded");
744776
const paths = installFsMocks({

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ function moveStagedInstallIntoCache(
906906
if (replaced) renameSync(aside, target);
907907
throw err;
908908
}
909-
if (replaced) rmSync(aside, { recursive: true, force: true });
909+
if (replaced) removeLeftover(aside);
910910
return join(target, relative(staged.path, staged.executablePath));
911911
}
912912

@@ -926,11 +926,19 @@ function removeInstallLeftovers(): boolean {
926926
throw err;
927927
}
928928
const leftovers = names.filter(isInstallLeftover);
929-
for (const name of leftovers)
930-
rmSync(join(CACHE_ROOT_DIR, name), { recursive: true, force: true });
929+
for (const name of leftovers) removeLeftover(join(CACHE_ROOT_DIR, name));
931930
return leftovers.length > 0;
932931
}
933932

933+
// A running Chrome keeps its debug.log open and Windows refuses to unlink it; the next sweep retries.
934+
function removeLeftover(path: string): void {
935+
try {
936+
rmSync(path, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
937+
} catch (err) {
938+
if (!isErrno(err, "EBUSY") && !isErrno(err, "EPERM")) throw err;
939+
}
940+
}
941+
934942
export async function sweepKilledInstalls(): Promise<void> {
935943
const names = existsSync(CACHE_ROOT_DIR) ? readdirSync(CACHE_ROOT_DIR) : [];
936944
if (!names.some(isInstallLeftover)) return;

0 commit comments

Comments
 (0)