From 390685bbe52c22b686fc0e3119b4ac7b1073c581 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Mon, 30 Jan 2023 11:22:55 +0100 Subject: [PATCH] fix: ignore not found contexts for console messages (#9595) --- packages/puppeteer-core/src/common/FrameManager.ts | 10 ++++++++-- packages/puppeteer-core/src/common/Page.ts | 12 +++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/puppeteer-core/src/common/FrameManager.ts b/packages/puppeteer-core/src/common/FrameManager.ts index 230fe8ba..48864ef7 100644 --- a/packages/puppeteer-core/src/common/FrameManager.ts +++ b/packages/puppeteer-core/src/common/FrameManager.ts @@ -174,12 +174,18 @@ export class FrameManager extends EventEmitter { contextId: number, session: CDPSession = this.#client ): ExecutionContext { - const key = `${session.id()}:${contextId}`; - const context = this.#contextIdToContext.get(key); + const context = this.getExecutionContextById(contextId, session); assert(context, 'INTERNAL ERROR: missing context with id = ' + contextId); return context; } + getExecutionContextById( + contextId: number, + session: CDPSession = this.#client + ): ExecutionContext | undefined { + return this.#contextIdToContext.get(`${session.id()}:${contextId}`); + } + page(): Page { return this.#page; } diff --git a/packages/puppeteer-core/src/common/Page.ts b/packages/puppeteer-core/src/common/Page.ts index f86abf6f..33018c42 100644 --- a/packages/puppeteer-core/src/common/Page.ts +++ b/packages/puppeteer-core/src/common/Page.ts @@ -747,10 +747,20 @@ export class CDPPage extends Page { // @see https://github.com/puppeteer/puppeteer/issues/3865 return; } - const context = this.#frameManager.executionContextById( + const context = this.#frameManager.getExecutionContextById( event.executionContextId, this.#client ); + if (!context) { + debugError( + new Error( + `ExecutionContext not found for a console message: ${JSON.stringify( + event + )}` + ) + ); + return; + } const values = event.args.map(arg => { return createJSHandle(context, arg); });