Skip to content

Commit

Permalink
chore: don't detach CdpSession (#12154)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning00Blade committed Apr 23, 2024
1 parent ed9d7dd commit f239130
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
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();
}
}
this.page().trustedEmitter.emit(PageEvent.FrameDetached, this);
Expand Down

0 comments on commit f239130

Please sign in to comment.