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

chore: don't detach CdpSession #12154

Merged
merged 2 commits into from
Apr 23, 2024
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
25 changes: 18 additions & 7 deletions packages/puppeteer-core/src/bidi/CDPSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class BidiCdpSession extends CDPSession {
static sessions = new Map<string, BidiCdpSession>();

#detached = false;
readonly #connection: BidiConnection | undefined = undefined;
readonly #connection?: BidiConnection;
readonly #sessionId = Deferred.create<string>();
readonly frame: BidiFrame;

Expand All @@ -41,11 +41,11 @@ export class BidiCdpSession extends CDPSession {
} else {
(async () => {
try {
const session = await connection.send('cdp.getSession', {
const {result} = await connection.send('cdp.getSession', {
context: frame._id,
});
this.#sessionId.resolve(session.result.session!);
BidiCdpSession.sessions.set(session.result.session!, this);
this.#sessionId.resolve(result.session!);
BidiCdpSession.sessions.set(result.session!, this);
} catch (error) {
this.#sessionId.reject(error as Error);
}
Expand Down Expand Up @@ -89,19 +89,30 @@ export class BidiCdpSession extends CDPSession {
}

override async detach(): Promise<void> {
if (this.#connection === undefined || this.#detached) {
if (
this.#connection === undefined ||
this.#connection.closed ||
this.#detached
) {
return;
}
try {
await this.frame.client.send('Target.detachFromTarget', {
sessionId: this.id(),
});
} finally {
BidiCdpSession.sessions.delete(this.id());
this.#detached = true;
this.onClose();
}
}

/**
* @internal
*/
onClose = (): void => {
BidiCdpSession.sessions.delete(this.id());
this.#detached = true;
};

override id(): string {
const value = this.#sessionId.value();
return typeof value === 'string' ? value : '';
Expand Down
2 changes: 1 addition & 1 deletion packages/puppeteer-core/src/bidi/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class BidiFrame extends Frame {
this.browsingContext.on('closed', () => {
for (const session of BidiCdpSession.sessions.values()) {
if (session.frame === this) {
void session.detach().catch(debugError);
session.onClose();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the BrowserContext is closed Mapper the session is already closed so we need to only remove it from the map.

}
}
this.page().trustedEmitter.emit(PageEvent.FrameDetached, this);
Expand Down