diff --git a/src/server/browserContext.ts b/src/server/browserContext.ts index e0368260b507a..0459aa2b06d0e 100644 --- a/src/server/browserContext.ts +++ b/src/server/browserContext.ts @@ -267,6 +267,10 @@ export abstract class BrowserContext extends EventEmitter { await this._doUpdateRequestInterception(); } + isClosingOrClosed() { + return this._closedStatus !== 'open'; + } + async close() { if (this._closedStatus === 'open') { this._closedStatus = 'closing'; diff --git a/src/server/chromium/crBrowser.ts b/src/server/chromium/crBrowser.ts index 7630ac4637ae4..b6ea6c965290c 100644 --- a/src/server/chromium/crBrowser.ts +++ b/src/server/chromium/crBrowser.ts @@ -164,8 +164,13 @@ export class CRBrowser extends Browser { this._crPages.set(targetInfo.targetId, crPage); crPage.pageOrError().then(pageOrError => { const page = crPage._page; - if (pageOrError instanceof Error) + if (pageOrError instanceof Error) { + // Initialization error could have happened because of + // context/browser closure. Just ignore the page. + if (context!.isClosingOrClosed()) + return; page._setIsError(); + } context!.emit(BrowserContext.Events.Page, page); if (opener) { opener.pageOrError().then(openerPage => { diff --git a/src/server/firefox/ffBrowser.ts b/src/server/firefox/ffBrowser.ts index db00b03a38a1a..1fed0d56287ee 100644 --- a/src/server/firefox/ffBrowser.ts +++ b/src/server/firefox/ffBrowser.ts @@ -108,8 +108,13 @@ export class FFBrowser extends Browser { ffPage.pageOrError().then(async pageOrError => { const page = ffPage._page; - if (pageOrError instanceof Error) + if (pageOrError instanceof Error) { + // Initialization error could have happened because of + // context/browser closure. Just ignore the page. + if (context.isClosingOrClosed()) + return; page._setIsError(); + } context.emit(BrowserContext.Events.Page, page); if (!opener) return; diff --git a/src/server/webkit/wkBrowser.ts b/src/server/webkit/wkBrowser.ts index 466b6df3b1082..d098ef688e93d 100644 --- a/src/server/webkit/wkBrowser.ts +++ b/src/server/webkit/wkBrowser.ts @@ -156,8 +156,13 @@ export class WKBrowser extends Browser { wkPage.pageOrError().then(async pageOrError => { const page = wkPage._page; - if (pageOrError instanceof Error) + if (pageOrError instanceof Error) { + // Initialization error could have happened because of + // context/browser closure. Just ignore the page. + if (context!.isClosingOrClosed()) + return; page._setIsError(); + } context!.emit(BrowserContext.Events.Page, page); if (!opener) return;