mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
fix: suppress init errors if the target is closed (#8947)
With #8520 Puppeteer is now aware of all targets it connects to. In order to have a not flaky init, Puppeteer waits for all existing targets to be configured during the connection process. This does not work well in case of concurrent connections because while one connection might initializing a target the other one might be closed it. In general, that is expected because we can only be eventually consistent about the target state but we also should not crash the init if some targets have been closed. This PR implements checks to see if the errors are caused by the target or session closures and suppresses them if it's the case.
This commit is contained in:
parent
c23c00a4c2
commit
cfaaa5e2c0
@ -131,7 +131,6 @@ export async function _connectToBrowser(
|
|||||||
targetFilter,
|
targetFilter,
|
||||||
isPageTarget
|
isPageTarget
|
||||||
);
|
);
|
||||||
await browser.pages();
|
|
||||||
return browser;
|
return browser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -445,3 +445,13 @@ function rewriteError(
|
|||||||
error.originalMessage = originalMessage ?? error.originalMessage;
|
error.originalMessage = originalMessage ?? error.originalMessage;
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
export function isTargetClosedError(err: Error): boolean {
|
||||||
|
return (
|
||||||
|
err.message.includes('Target closed') ||
|
||||||
|
err.message.includes('Session closed')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
@ -19,7 +19,7 @@ import {assert} from '../util/assert.js';
|
|||||||
import {createDebuggableDeferredPromise} from '../util/DebuggableDeferredPromise.js';
|
import {createDebuggableDeferredPromise} from '../util/DebuggableDeferredPromise.js';
|
||||||
import {DeferredPromise} from '../util/DeferredPromise.js';
|
import {DeferredPromise} from '../util/DeferredPromise.js';
|
||||||
import {isErrorLike} from '../util/ErrorLike.js';
|
import {isErrorLike} from '../util/ErrorLike.js';
|
||||||
import {CDPSession} from './Connection.js';
|
import {CDPSession, isTargetClosedError} from './Connection.js';
|
||||||
import {EventEmitter} from './EventEmitter.js';
|
import {EventEmitter} from './EventEmitter.js';
|
||||||
import {EVALUATION_SCRIPT_URL, ExecutionContext} from './ExecutionContext.js';
|
import {EVALUATION_SCRIPT_URL, ExecutionContext} from './ExecutionContext.js';
|
||||||
import {Frame} from './Frame.js';
|
import {Frame} from './Frame.js';
|
||||||
@ -172,11 +172,7 @@ export class FrameManager extends EventEmitter {
|
|||||||
]);
|
]);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// The target might have been closed before the initialization finished.
|
// The target might have been closed before the initialization finished.
|
||||||
if (
|
if (isErrorLike(error) && isTargetClosedError(error)) {
|
||||||
isErrorLike(error) &&
|
|
||||||
(error.message.includes('Target closed') ||
|
|
||||||
error.message.includes('Session closed'))
|
|
||||||
) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,11 @@ import {
|
|||||||
import {isErrorLike} from '../util/ErrorLike.js';
|
import {isErrorLike} from '../util/ErrorLike.js';
|
||||||
import {Accessibility} from './Accessibility.js';
|
import {Accessibility} from './Accessibility.js';
|
||||||
import {Browser, BrowserContext} from './Browser.js';
|
import {Browser, BrowserContext} from './Browser.js';
|
||||||
import {CDPSession, CDPSessionEmittedEvents} from './Connection.js';
|
import {
|
||||||
|
CDPSession,
|
||||||
|
CDPSessionEmittedEvents,
|
||||||
|
isTargetClosedError,
|
||||||
|
} from './Connection.js';
|
||||||
import {ConsoleMessage, ConsoleMessageType} from './ConsoleMessage.js';
|
import {ConsoleMessage, ConsoleMessageType} from './ConsoleMessage.js';
|
||||||
import {Coverage} from './Coverage.js';
|
import {Coverage} from './Coverage.js';
|
||||||
import {Dialog} from './Dialog.js';
|
import {Dialog} from './Dialog.js';
|
||||||
@ -470,7 +474,15 @@ export class Page extends EventEmitter {
|
|||||||
);
|
);
|
||||||
await page.#initialize();
|
await page.#initialize();
|
||||||
if (defaultViewport) {
|
if (defaultViewport) {
|
||||||
await page.setViewport(defaultViewport);
|
try {
|
||||||
|
await page.setViewport(defaultViewport);
|
||||||
|
} catch (err) {
|
||||||
|
if (isErrorLike(err) && isTargetClosedError(err)) {
|
||||||
|
debugError(err);
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return page;
|
return page;
|
||||||
}
|
}
|
||||||
@ -645,11 +657,19 @@ export class Page extends EventEmitter {
|
|||||||
};
|
};
|
||||||
|
|
||||||
async #initialize(): Promise<void> {
|
async #initialize(): Promise<void> {
|
||||||
await Promise.all([
|
try {
|
||||||
this.#frameManager.initialize(this.#target._targetId),
|
await Promise.all([
|
||||||
this.#client.send('Performance.enable'),
|
this.#frameManager.initialize(this.#target._targetId),
|
||||||
this.#client.send('Log.enable'),
|
this.#client.send('Performance.enable'),
|
||||||
]);
|
this.#client.send('Log.enable'),
|
||||||
|
]);
|
||||||
|
} catch (err) {
|
||||||
|
if (isErrorLike(err) && isTargetClosedError(err)) {
|
||||||
|
debugError(err);
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async #onFileChooser(
|
async #onFileChooser(
|
||||||
|
Loading…
Reference in New Issue
Block a user