From 9a0f56f9fe83c57b62a8ba66f9da26b231d64f8d Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 1 Dec 2026 14:44:02 +0530 Subject: [PATCH 1/2] fix: keep the server up when Lighthouse rejects after Chromium closes --- src/server.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/server.ts b/src/server.ts index 11c0872..f560b4e 100644 --- a/src/server.ts +++ b/src/server.ts @@ -7,6 +7,13 @@ import { } from "./routes"; import { Router } from "./utils/router"; +// Lighthouse sends CDP commands from event listeners nobody awaits, so when +// Chromium closes mid-report (a memory budget restart, a crash) they reject +// outside any request. The report itself already fails; keep the server up. +process.on("unhandledRejection", (reason) => { + console.error("Unhandled rejection:", reason); +}); + const router = new Router(); router.add("POST", "/v1/screenshots", handleScreenshotsRequest); router.add("POST", "/v1/reports", handleReportsRequest); From 37e14e10350f3eeb1486d9918498b23d0d758909 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 1 Dec 2026 14:49:58 +0530 Subject: [PATCH 2/2] fix: fail a report when Chromium closes or the audit stalls --- src/routes/reports.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/routes/reports.ts b/src/routes/reports.ts index 49a497c..f970c9b 100755 --- a/src/routes/reports.ts +++ b/src/routes/reports.ts @@ -15,6 +15,8 @@ import { } from "../utils/memory.js"; import { assertAllowedUrl } from "../utils/ssrf.js"; +const AUDIT_TIMEOUT_MS = 120_000; + export async function handleReportsRequest(req: Request): Promise { let context: BrowserContext | undefined; let guard: EgressGuard | undefined; @@ -90,7 +92,22 @@ export async function handleReportsRequest(req: Request): Promise { seo: 0, }; - const results = await playAudit({ + // Lighthouse never settles when Chromium closes under it, and can stall on + // a page that never goes idle, so the audit gets a deadline of its own. + const closed = new Promise((_, reject) => { + context?.once("close", () => + reject(new Error("Chromium closed during the report.")), + ); + }); + let timer: ReturnType | undefined; + const stalled = new Promise((_, reject) => { + timer = setTimeout( + () => reject(new Error("Report timed out.")), + AUDIT_TIMEOUT_MS, + ); + }); + + const audit = playAudit({ reports: { formats: { html: body.html, @@ -104,6 +121,13 @@ export async function handleReportsRequest(req: Request): Promise { thresholds, }); + // Whichever loses the race settles later with nobody awaiting it. + for (const pending of [audit, closed]) pending.catch(() => {}); + + const results = await Promise.race([audit, closed, stalled]).finally(() => + clearTimeout(timer), + ); + // Lighthouse reloads the page itself, so check what that navigation did. navigations.assertAllowed();