diff --git a/packages/puppeteer-core/src/cdp/FrameManager.ts b/packages/puppeteer-core/src/cdp/FrameManager.ts index f7c7d0a99f8..ed3d2f0efb0 100644 --- a/packages/puppeteer-core/src/cdp/FrameManager.ts +++ b/packages/puppeteer-core/src/cdp/FrameManager.ts @@ -490,9 +490,7 @@ export class FrameManager extends EventEmitter { contextPayload, world ); - if (world) { - world.setContext(context); - } + world.setContext(context); const key = `${session.id()}:${contextPayload.id}`; this.#contextIdToContext.set(key, context); context.once('disposed', () => { @@ -502,7 +500,6 @@ export class FrameManager extends EventEmitter { return; } this.#contextIdToContext.delete(key); - context._world.clearContext(); }); } diff --git a/packages/puppeteer-core/src/cdp/IsolatedWorld.ts b/packages/puppeteer-core/src/cdp/IsolatedWorld.ts index 29ed52cdf47..6e1ddc444b8 100644 --- a/packages/puppeteer-core/src/cdp/IsolatedWorld.ts +++ b/packages/puppeteer-core/src/cdp/IsolatedWorld.ts @@ -64,20 +64,19 @@ export class IsolatedWorld extends Realm { return this.#frameOrWorker.client; } - clearContext(): void { - // The message has to match the CDP message expected by the WaitTask class. - this.#context?.reject(new Error('Execution context was destroyed')); - this.#context = Deferred.create(); - if ('clearDocumentHandle' in this.#frameOrWorker) { - this.#frameOrWorker.clearDocumentHandle(); - } - } - setContext(context: ExecutionContext): void { const existingContext = this.#context.value(); if (existingContext instanceof ExecutionContext) { existingContext[disposeSymbol](); } + context.once('disposed', () => { + // The message has to match the CDP message expected by the WaitTask class. + this.#context?.reject(new Error('Execution context was destroyed')); + this.#context = Deferred.create(); + if ('clearDocumentHandle' in this.#frameOrWorker) { + this.#frameOrWorker.clearDocumentHandle(); + } + }); this.#context.resolve(context); void this.taskManager.rerunAll(); }