Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: destroy puppeteer utility on context destruction #10672

Merged
merged 1 commit into from Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/puppeteer-core/src/common/bidi/Browser.ts
Expand Up @@ -54,6 +54,7 @@ export class Browser extends BrowserBase {
'browsingContext',
'network',
'log',
'script',
];
static readonly subscribeCdpEvents: Bidi.Cdp.EventNames[] = [
// Coverage
Expand Down
17 changes: 17 additions & 0 deletions packages/puppeteer-core/src/common/bidi/Realm.ts
Expand Up @@ -7,6 +7,7 @@ import {scriptInjector} from '../ScriptInjector.js';
import {EvaluateFunc, HandleFor} from '../types.js';
import {
PuppeteerURL,
debugError,
getSourcePuppeteerURLIfAvailable,
isString,
} from '../util.js';
Expand Down Expand Up @@ -46,6 +47,22 @@ export class Realm extends EventEmitter {

setFrame(frame: Frame): void {
this.#frame = frame;

// TODO(jrandolf): We should try to find a less brute-force way of doing
// this.
this.connection.on(
Bidi.ChromiumBidi.Script.EventNames.RealmDestroyed,
async () => {
const promise = this.internalPuppeteerUtil;
this.internalPuppeteerUtil = undefined;
try {
const util = await promise;
await util?.dispose();
} catch (error) {
debugError(error);
}
}
);
}

protected internalPuppeteerUtil?: Promise<JSHandle<PuppeteerUtil>>;
Expand Down
9 changes: 3 additions & 6 deletions packages/puppeteer-core/src/common/bidi/Sandbox.ts
Expand Up @@ -60,7 +60,6 @@ export interface SandboxChart {
* @internal
*/
export class Sandbox implements RealmBase {
#document?: ElementHandle<Document>;
#realm: Realm;

#timeoutSettings: TimeoutSettings;
Expand All @@ -76,13 +75,11 @@ export class Sandbox implements RealmBase {
}

async document(): Promise<ElementHandle<Document>> {
if (this.#document) {
return this.#document;
}
this.#document = await this.#realm.evaluateHandle(() => {
// TODO(jrandolf): We should try to cache this because we need to dispose
// this when it's unused.
return await this.#realm.evaluateHandle(() => {
jrandolf marked this conversation as resolved.
Show resolved Hide resolved
return document;
});
return this.#document;
}

async $<Selector extends string>(
Expand Down
42 changes: 42 additions & 0 deletions test/TestExpectations.json
Expand Up @@ -341,12 +341,48 @@
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[browser.spec] Browser specs Browser.target should return browser target",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[browsercontext.spec] BrowserContext should close all belonging targets once closing context",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[browsercontext.spec] BrowserContext should create new incognito context",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[browsercontext.spec] BrowserContext should have default context",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[browsercontext.spec] BrowserContext should timeout waiting for a non-existent target",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[browsercontext.spec] BrowserContext should wait for a target",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[browsercontext.spec] BrowserContext window.open should use parent tab context",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[CDPSession.spec] Target.createCDPSession *",
"platforms": ["darwin", "linux", "win32"],
Expand Down Expand Up @@ -3899,6 +3935,12 @@
"parameters": ["cdp", "chrome", "new-headless"],
"expectations": ["FAIL", "PASS"]
},
{
"testIdPattern": "[click.spec] Page.click should click the button after navigation",
"platforms": ["darwin"],
"parameters": ["chrome", "headless", "webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[click.spec] Page.click should click the button with fixed position inside an iframe",
"platforms": ["darwin", "linux", "win32"],
Expand Down