Skip to content

Commit 2f3b7e7

Browse files
committed
fix: don't let a hanging page.close() block the request cleanup
Puppeteer 25 can hang `page.close()` indefinitely when the page's navigation was aborted (e.g. on a navigation timeout), and the request cleanup awaited it without any guard, blocking the crawler forever. Bound it with a 5s timeout.
1 parent 4b0c394 commit 2f3b7e7

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

packages/browser-crawler/src/internals/browser-crawler.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import { addTimeoutToPromise, tryCancel } from '@apify/timeout';
4949

5050
import type { BrowserLaunchContext } from './browser-launcher';
5151

52+
const PAGE_CLOSE_TIMEOUT_MILLIS = 5000;
53+
5254
export interface BrowserCrawlingContext<
5355
Crawler = unknown,
5456
Page extends CommonPage = CommonPage,
@@ -462,7 +464,12 @@ export abstract class BrowserCrawler<
462464

463465
// Page creation may be aborted
464466
if (page) {
465-
await page.close().catch((error: Error) => this.log.debug('Error while closing page', { error }));
467+
// Puppeteer 25+ can hang `page.close()` indefinitely when the page's navigation was aborted, don't let it block the crawler.
468+
await addTimeoutToPromise(
469+
async () => page.close(),
470+
PAGE_CLOSE_TIMEOUT_MILLIS,
471+
`page.close() timed out after ${PAGE_CLOSE_TIMEOUT_MILLIS / 1000} seconds`,
472+
).catch((error: Error) => this.log.debug('Error while closing page', { error }));
466473
}
467474
}
468475

0 commit comments

Comments
 (0)