Skip to content

BrowserCrawler closes ctx.page before errorHandler runs on navigation errors #3647

@nikitachapovskii-dev

Description

@nikitachapovskii-dev

Discovered in WCC PR

but turns out it closes the page on error before errorHandler runs ... _handleNavigationTimeout which closes crawlingContext.page.close() is being called on any navigation error

on actor side errorHandler can't reliably interact with ctx.page after a navigation error because the page is already closed by the time it runs.

The culprit is BrowserCrawler._handleNavigationTimeout

protected async _handleNavigationTimeout(crawlingContext: Context, error: Error): Promise<void> {
const { session } = crawlingContext;
if (error && error.constructor.name === 'TimeoutError') {
handleRequestTimeout({ session, errorMessage: error.message });
}
await crawlingContext.page.close();
}

despite the name, it's called on every navigation error (timeout, network failure, Download is starting, frame detached, ...) and unconditionally does await page.close().

That close is also redundant: _cleanupContext already closes the page in the finally of _runRequestFunction. So the eager close just races the user's errorHandler (and, via activePages--, the browser pool's killer) for no real win.

Where eager close actually helps

Going through the cases:

  • TimeoutError — page is probably hung on resources, eager close releases them immediately. Genuinely useful.
  • net::ERR_* (DNS/refused/SSL/etc.) — nothing was loaded, nothing to release.
  • Download is starting — page is healthy, browser holds the captured Download.
  • frame detached / page already closed_cleanupContext handles it idempotently anyway.

Only the timeout case justifies eager close.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working.t-toolingIssues with this label are in the ownership of the tooling team.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions