Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/routes/reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> {
let context: BrowserContext | undefined;
let guard: EgressGuard | undefined;
Expand Down Expand Up @@ -90,7 +92,22 @@ export async function handleReportsRequest(req: Request): Promise<Response> {
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<never>((_, reject) => {
context?.once("close", () =>
reject(new Error("Chromium closed during the report.")),
);
});
let timer: ReturnType<typeof setTimeout> | undefined;
const stalled = new Promise<never>((_, reject) => {
timer = setTimeout(
() => reject(new Error("Report timed out.")),
AUDIT_TIMEOUT_MS,
);
});

const audit = playAudit({
reports: {
formats: {
html: body.html,
Expand All @@ -104,6 +121,13 @@ export async function handleReportsRequest(req: Request): Promise<Response> {
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();

Expand Down
7 changes: 7 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Comment on lines +13 to +15

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a regression test for process-level rejection recovery

Could we add a regression test that causes a CDP rejection during an active report and then verifies the server still handles a subsequent request? The existing browser recovery test kills Chromium before a screenshot, so it does not reliably exercise the unhandled-rejection behavior added here.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/server.ts
Line: 13-15

Comment:
**Add a regression test for process-level rejection recovery**

Could we add a regression test that causes a CDP rejection during an active report and then verifies the server still handles a subsequent request? The existing browser recovery test kills Chromium before a screenshot, so it does not reliably exercise the unhandled-rejection behavior added here.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

🟡 Minor · testing · Reply if this doesn't apply.


const router = new Router();
router.add("POST", "/v1/screenshots", handleScreenshotsRequest);
router.add("POST", "/v1/reports", handleReportsRequest);
Expand Down
Loading