Skip to content

Chrome DevTools for coding agents

fix: do not throw synchronously when a CDP session is gone (#2466)

Dmitriusanba4fe3eaa4c2
diff --git a/src/devtools/DevToolsConnectionAdapter.ts b/src/devtools/DevToolsConnectionAdapter.ts
index 24a737af..3c8969f2 100644
--- a/src/devtools/DevToolsConnectionAdapter.ts
+++ b/src/devtools/DevToolsConnectionAdapter.ts
@@ -56,7 +56,17 @@ export class PuppeteerDevToolsConnection
}
const session = this.#connection.session(sessionId);
if (!session) {
- throw new Error('Unknown session ' + sessionId);
+ // A session can go away while commands for it are still in flight, for
+ // example when a page is closed while its source maps are loading. That
+ // is normal, so report it the same way a failed command is reported.
+ // Throwing here would escape synchronously out of a method declared to
+ // return a promise, past the generated agent code, and end the process.
+ return Promise.resolve({
+ error: {
+ code: -32000,
+ message: 'Unknown session ' + sessionId,
+ } as DevTools.CDPConnection.CDPError,
+ });
}
// Rolled protocol version between puppeteer and DevTools doesn't necessarily match
/* eslint-disable @typescript-eslint/no-explicit-any */
diff --git a/tests/devtools/DevToolsConnectionAdapter.test.ts b/tests/devtools/DevToolsConnectionAdapter.test.ts
new file mode 100644
index 00000000..875463e7
--- /dev/null
+++ b/tests/devtools/DevToolsConnectionAdapter.test.ts
@@ -0,0 +1,68 @@
+/**
+ * @license
+ * Copyright 2026 Google LLC
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+import assert from 'node:assert';
+import {describe, it} from 'node:test';
+
+import {PuppeteerDevToolsConnection} from '../../src/devtools/DevToolsConnectionAdapter.js';
+import type {CDPSession} from '../../src/third_party/index.js';
+import {mockListener} from '../utils.js';
+
+function getMockSession(sessionsById: Record<string, unknown>): CDPSession {
+ const connection = {
+ session(id: string) {
+ return sessionsById[id];
+ },
+ };
+ return {
+ ...mockListener(),
+ id() {
+ return 'root-session';
+ },
+ connection() {
+ return connection;
+ },
+ } as unknown as CDPSession;
+}
+
+describe('PuppeteerDevToolsConnection', () => {
+ it('reports an unknown session as an error instead of throwing', async () => {
+ const session = getMockSession({'root-session': {}});
+ const connection = new PuppeteerDevToolsConnection(session);
+
+ // The page this session belonged to is gone, which is what happens when a
+ // tab closes while commands for it are still in flight.
+ const response = await connection.send(
+ 'Network.loadNetworkResource' as never,
+ {} as never,
+ 'session-that-went-away',
+ );
+
+ assert.ok('error' in response, 'expected an error response');
+ assert.match(response.error.message, /Unknown session/);
+ });
+
+ it('forwards commands to the session it belongs to', async () => {
+ const calls: string[] = [];
+ const target = {
+ send(method: string) {
+ calls.push(method);
+ return Promise.resolve({ok: true});
+ },
+ };
+ const session = getMockSession({'a-session': target});
+ const connection = new PuppeteerDevToolsConnection(session);
+
+ const response = await connection.send(
+ 'Page.enable' as never,
+ {} as never,
+ 'a-session',
+ );
+
+ assert.deepStrictEqual(calls, ['Page.enable']);
+ assert.deepStrictEqual(response, {result: {ok: true}});
+ });
+});