Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,20 @@ export class BrowserManager {
this.activePageIndex = this.pages.length > 0 ? this.pages.length - 1 : 0;
}

/**
* Safely disconnect from a CDP session without closing the external browser process.
* Playwright's browser.close() sends Browser.close command which kills the process.
* Accessing the underlying connection allows us to just close the WebSocket.
*/
private async safelyDisconnectCdp(browser: Browser): Promise<void> {
const b = browser as any;
if (b._connection && typeof b._connection.close === 'function') {
b._connection.close();
} else {
await browser.close().catch(() => {});
}
}

/**
* Connect to a running browser via CDP (Chrome DevTools Protocol)
* @param cdpEndpoint Either a port number (as string) or a full WebSocket URL (ws:// or wss://)
Expand Down Expand Up @@ -1490,7 +1504,7 @@ export class BrowserManager {
this.activePageIndex = 0;
} catch (error) {
// Clean up browser connection if validation or setup failed
await browser.close().catch(() => {});
await this.safelyDisconnectCdp(browser);
throw error;
}
}
Expand Down Expand Up @@ -2444,7 +2458,7 @@ export class BrowserManager {
} else if (this.cdpEndpoint !== null) {
// CDP: only disconnect, don't close external app's pages
if (this.browser) {
await this.browser.close().catch(() => {});
await this.safelyDisconnectCdp(this.browser);
this.browser = null;
}
} else {
Expand Down