Skip to content

Commit 3f7e082

Browse files
Serhatcckclaude
andcommitted
fix(hackbrowser): guard request.allHeaders() so a closed target can't crash the worker (#104)
The page.on("request", async …) capture handler awaited request.allHeaders() without a try/catch. In headed mode a page/context/browser can close mid-crawl (a popup/tab closing, a navigation) while requests are still in flight; allHeaders() then rejects with "Target page, context or browser has been closed". Because the handler is a fire-and-forget async listener and the worker installs no unhandledRejection guard, that rejection takes down the whole worker process (exit 1) — the crawl ends after only a fraction of the target. Verified from code + repro: allHeaders() is the SOLE unguarded await in the handler (the UI -context and response branches below are already try/caught); Bun terminates the process on an unhandled rejection; the worker has no safety net. Wrap the call and drop the capture on a closed target instead (a request whose headers never arrived isn't a usable capture), with a debug line naming the in-flight URL for diagnosis. Repro confirms no unhandled rejection after the fix; 99/99 tests pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 2b05236 commit 3f7e082

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

packages/hackbrowser/src/agent.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,15 @@ function setupRequestInterceptor(
476476
}
477477

478478
const method = request.method()
479-
const headers = await request.allHeaders()
479+
let headers: Record<string, string>
480+
try {
481+
headers = await request.allHeaders()
482+
} catch {
483+
// Target/page/browser closed mid-flight → drop this capture instead of letting the
484+
// unhandled rejection crash the worker. (The response branch below is already guarded.)
485+
log.debug("request capture dropped — target closed mid-flight", { url })
486+
return
487+
}
480488
const postData = request.postData() ?? null
481489
const raw = buildRawRequest(method, url, headers, postData)
482490

0 commit comments

Comments
 (0)