diff --git a/packages/puppeteer-core/src/api/Realm.ts b/packages/puppeteer-core/src/api/Realm.ts index eee1f2c1dd9..ee234932d5c 100644 --- a/packages/puppeteer-core/src/api/Realm.ts +++ b/packages/puppeteer-core/src/api/Realm.ts @@ -95,10 +95,14 @@ export abstract class Realm implements Disposable { #disposed = false; /** @internal */ - [disposeSymbol](): void { + dispose(): void { this.#disposed = true; this.taskManager.terminateAll( new Error('waitForFunction failed: frame got detached.') ); } + /** @internal */ + [disposeSymbol](): void { + this.dispose(); + } } diff --git a/packages/puppeteer-core/src/bidi/Realm.ts b/packages/puppeteer-core/src/bidi/Realm.ts index 1027941e2fa..45e2fb73f01 100644 --- a/packages/puppeteer-core/src/bidi/Realm.ts +++ b/packages/puppeteer-core/src/bidi/Realm.ts @@ -53,6 +53,7 @@ export abstract class BidiRealm extends Realm { protected initialize(): void { this.realm.on('destroyed', ({reason}) => { this.taskManager.terminateAll(new Error(reason)); + this.dispose(); }); this.realm.on('updated', () => { this.internalPuppeteerUtil = undefined; @@ -224,6 +225,10 @@ export abstract class BidiRealm extends Realm { } async destroyHandles(handles: Array>): Promise { + if (this.disposed) { + return; + } + const handleIds = handles .map(({id}) => { return id;