Repository navigation
fix(browser): retry only transient connection failures in connect - #3375
cheyoung1983-sudo wants to merge 2 commits into
Conversation
|
Hi! Could a maintainer please approve the pending workflow runs for this fork PR (Compile and run tests, Check code before submitting, Conventional Commit, Memory leak tests are awaiting approval)? The feature request is #3374, and the CLA check is green. All of these pass locally on Node 24. Thanks! |
Wrap puppeteer.connect() in connectWithRetry with bounded attempts and exponential backoff. Only transient transport errors (ECONNREFUSED, ECONNRESET, ETIMEDOUT, socket hang up, ...) are retried. Permanent errors such as invalid URLs and HTTP rejections of the WebSocket upgrade (e.g. --auto-connect consent denied) fail immediately. Refs ChromeDevTools#3374
Inspect AggregateError.errors (dual-stack fetch failures), let permanent signals win anywhere in the cause chain, match error codes in messages on word boundaries, clamp attempts/delay, and document the backoff schedule. Adds tests for nested causes, aggregates, cycles and attempts < 1.
0b2fc72 to
dfca566
Compare
| throw error; | ||
| } | ||
| const delay = Math.max(0, baseDelayMs) * 2 ** (attempt - 1); | ||
| logger?.( |
There was a problem hiding this comment.
Logging the error can leak credentials and query strings to logs. Log only the transient error code and attempt number, or redact URLs with redactUrlForLogging.
| * the WebSocket upgrade such as the 403 Chrome returns when the user denies the | ||
| * `--auto-connect` consent dialog — is treated as permanent and not retried. | ||
| */ | ||
| export function isTransientConnectError(error: unknown): boolean { |
There was a problem hiding this comment.
isTransientConnectErrorFails on All Real puppeteer.connect() Modes
In live runs against puppeteer-core@25.16.0 on Node 24, isTransientConnectError returns false on ECONNREFUSED for every connection mode:
browserURLmode (--browserUrl):getWSEndpointcallsglobalThis.fetch().- On every network error,
fetch()rejects withTypeError('fetch failed', {cause}). - Puppeteer prepends
Failed to fetch browser webSocket URL...to thatTypeErrorand rethrows it. classifyConnectErrorcheckserror instanceof TypeErrorfirst and returns'permanent'before readingerror.cause.- Also, when a server resets an HTTP socket mid-response, Undici sets
code: 'UND_ERR_SOCKET'(other side closed).TRANSIENT_ERROR_CODESomitsUND_ERR_SOCKET.
wsEndpointandautoConnect+userDataDirmodes:NodeWebSocketTransport.createcallsws.addEventListener('error', reject).- The
wspackage rejects with anErrorEvent(event instanceof Error === false) and places theErrororAggregateErrorinevent.error. - On dual-stack
localhost,event.messageis""andevent.errorholds theAggregateError. classifyConnectErrorchecks!(error instanceof Error)and returns'unknown'.
autoConnect+channelmode:BrowserConnector.jscatches thewsErrorEventand throwsnew Error('Could not find DevToolsActivePort...', {cause: event}).classifyConnectErrorrecurses intoerror.cause, sees the non-ErrorErrorEvent, and returns'unknown'.
Unit tests in browser.test.ts missed these bugs because they build new Error('fetch failed', ...) instead of new TypeError(...) and never construct ws ErrorEvent objects.
|
|
||
| type Classification = 'transient' | 'permanent' | 'unknown'; | ||
|
|
||
| function classifyConnectError( |
There was a problem hiding this comment.
Every transient failure from Node fetch() and ws produces leaf errors with a structured .code property. Every permanent failure (ERR_INVALID_URL, SyntaxError, HTTP 403 consent denial with code: undefined, and ENOENT/EACCES on DevToolsActivePort) has either code: undefined or a non-transient .code.
A private leaf-code check inside BrowserManager.ts eliminates all substring lists, regexes, as casts, and test-only exports:
const TRANSIENT_CONNECT_CODES = new Set([
'ECONNREFUSED',
'ECONNRESET',
'EPIPE',
'EAI_AGAIN',
'UND_ERR_SOCKET',
]);
const CONNECT_RETRY_ATTEMPTS = 3;
const CONNECT_RETRY_BASE_DELAY_MS = 250;
function extractTransientConnectCode(
error: unknown,
seen = new Set<unknown>(),
): string | undefined {
if (typeof error !== 'object' || error === null || seen.has(error)) {
return undefined;
}
seen.add(error);
if (!(error instanceof Error)) {
return 'error' in error
? extractTransientConnectCode(error.error, seen)
: undefined;
}
if (error instanceof AggregateError) {
if (error.errors.length === 0) {
return undefined;
}
let firstCode: string | undefined;
for (const inner of error.errors) {
const code = extractTransientConnectCode(inner, seen);
if (!code) {
return undefined;
}
firstCode ??= code;
}
return firstCode;
}
if (error.cause !== undefined) {
return extractTransientConnectCode(error.cause, seen);
}
if (
'code' in error &&
typeof error.code === 'string' &&
TRANSIENT_CONNECT_CODES.has(error.code)
) {
return error.code;
}
return undefined;
}|
closing as stale, please re-open if the feedback is addressd |
Refactor BrowserManager connect retry logic to inspect error.cause chains, AggregateError.errors, and ws ErrorEvent objects for leaf transient error codes (ECONNREFUSED, ECONNRESET, UND_ERR_SOCKET, EPIPE, EAI_AGAIN, ETIMEDOUT). Permanent errors fail immediately without retry. Log statements output only attempt count and error codes to prevent credential leaks. Fixes ChromeDevTools#3374 Refs ChromeDevTools#3375
|
The feedback on transient connection retries in
The updated code and unit tests have been pushed to the fork branch |
Refactor BrowserManager connect retry logic to inspect error.cause chains, AggregateError.errors, and ws ErrorEvent objects for leaf transient error codes (ECONNREFUSED, ECONNRESET, UND_ERR_SOCKET, EPIPE, EAI_AGAIN, ETIMEDOUT). Permanent errors fail immediately without retry. Log statements output only attempt count and error codes to prevent credential leaks. Fixes ChromeDevTools#3374 Refs ChromeDevTools#3375, ChromeDevTools#3557
…romeDevTools#3557) ## Summary Retry only **transient** connection failures in `BrowserManager` when calling `puppeteer.connect`. Fixes ChromeDevTools#3374. Reworked per @wolfib's review: rebased onto current `main` as a single commit, with the unrelated `PageCollector`/`browser.ts` changes dropped. ## Behavior - A private leaf-code extractor in `BrowserManager.ts` (the one suggested on ChromeDevTools#3375) reads the error's code from the root of the `cause` chain, from `ws` `ErrorEvent.error`, and from every entry of an `AggregateError`. It retries only `ECONNREFUSED`, `ECONNRESET`, `EPIPE`, `EAI_AGAIN`, and `UND_ERR_SOCKET`. There is no `instanceof TypeError` check and no substring list, so `--browserUrl` fetch failures (`TypeError('fetch failed', {cause})`) are classified by their root cause. - Permanent errors fail immediately: invalid URL, the 403 consent denial, a permanent root cause under a transient-looking wrapper (e.g. `EACCES`), and mixed `AggregateError`s. - Up to 3 attempts with 250ms then 500ms backoff. The success path is unchanged, and the last error is rethrown unmodified. - `#ensureBrowserLocked()` passes an abort check into `#connect()`, so retries stop after `close()` or `abandonPendingAttempt()`. - Logs only the attempt number, the code, and the delay. - No new exports. ## Test plan - [x] `tests/browser.test.ts`, run through `BrowserManager.ensureBrowser()`: `--browserUrl` `TypeError` fetch failure then success; `ws` `ErrorEvent` wrapping an all-transient `AggregateError` then success; exhausting 3 attempts; no retry for invalid URL, 403, `EACCES` root cause, or a mixed aggregate; no further attempts after `close()` or `abandonPendingAttempt()` during backoff. - [x] Local on Node 24: `npm run sync`, `npm test`, `npm run check-format`, and `npm run gen` (no diff) all pass.
Summary
Retry only transient connection failures in
BrowserManagerpuppeteer.connect, replacing closed #3368 and addressing @wolfib feedback. Fixes #3374.Behavior
causechains andAggregateError.errors.--auto-connectconsent denial (e.g. unexpected server response / 403).causechain andAggregateError.errors; permanent signals (403 consent denial, invalid URL, TypeError) always win. Tests cover nested causes, aggregates, cycles and attempts below 1.Non-goals
Does not retry permanent config errors or intentional consent denial.
Test plan
npm ci, bundle, fulltest:no-build, format, gen, verify-server-json-version, memory profile — all pass on Node 24