Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

Do not re-await timed-out or aborted debugger attachments during teardown

Emre Sokulluf6d0521002bd
diff --git a/src/chrome/src/cdp/cdp-client.js b/src/chrome/src/cdp/cdp-client.js
index 67b126893..8558dff7d 100644
--- a/src/chrome/src/cdp/cdp-client.js
+++ b/src/chrome/src/cdp/cdp-client.js
@@ -199,6 +199,12 @@ export class CDPClient {
return;
}
+ if (attachPromise.cancelled) {
+ try { chrome.debugger.detach({ tabId }, () => {}); } catch {}
+ reject(new Error('Debugger attachment was cancelled'));
+ return;
+ }
+
const session = { tabId, attached: true };
this.sessions.set(tabId, session);
resolve(session);
@@ -222,7 +228,7 @@ export class CDPClient {
this.pendingDialogs.delete(tabId);
if (!this.sessions.has(tabId)) {
const pendingAttach = this.attachPromises.get(tabId);
- if (!pendingAttach) return;
+ if (!pendingAttach || pendingAttach.cancelled) return;
try {
await pendingAttach;
} catch {
@@ -287,13 +293,19 @@ export class CDPClient {
this.dialogRuns.set(tabId, owner);
signal?.addEventListener('abort', owner.onAbort, { once: true });
let timer;
+ const markPendingAttachCancelled = () => {
+ const pendingAttach = this.attachPromises.get(tabId);
+ if (pendingAttach) pendingAttach.cancelled = true;
+ };
const interrupted = new Promise((_, reject) => {
owner.cancelStartup = () => {
+ markPendingAttachCancelled();
const error = new Error('Stopped during browser dialog setup');
error.name = 'AbortError';
reject(error);
};
timer = setTimeout(() => {
+ markPendingAttachCancelled();
const error = new Error('Browser dialog setup timed out. Dismiss any existing browser dialog and try again.');
error.code = 'dialog_startup_timeout';
reject(error);
@@ -311,6 +323,7 @@ export class CDPClient {
// must release the run even when Chrome never answers this command.
await Promise.race([this.sendCommand(tabId, 'Page.enable'), interrupted]);
} catch (error) {
+ markPendingAttachCancelled();
if (this.dialogRuns.get(tabId) === owner) this.stopDialogHandling(tabId);
throw error;
} finally {
diff --git a/test/browser-dialogs.mjs b/test/browser-dialogs.mjs
index 64c380080..c63669538 100644
--- a/test/browser-dialogs.mjs
+++ b/test/browser-dialogs.mjs
@@ -213,6 +213,63 @@ test('cached dialogs and ended navigation cannot inherit authorization', async (
assert.ok(calls.filter(call => call[1] === 'Page.handleJavaScriptDialog').every(call => !call[2].accept));
});
+test('cleanupRun and teardown do not re-await timed-out or aborted debugger attachments', async () => {
+ const previousChrome = globalThis.chrome;
+ const area = { get: async () => ({}), set: async () => {}, remove: async () => {} };
+ let detaches = 0;
+ let attachCallback = null;
+ globalThis.chrome = {
+ storage: { local: area, session: area },
+ runtime: { getURL: value => value },
+ tabs: { get: async id => ({ id, url: 'https://example.com/' }) },
+ debugger: {
+ attach: (_target, _version, callback) => { attachCallback = callback; },
+ detach: (_target, callback) => { detaches++; callback?.(); },
+ onEvent: { addListener: () => {} },
+ onDetach: { addListener: () => {} },
+ },
+ };
+ const { Agent } = await import('../src/chrome/src/agent/agent.js');
+ const { cdpClient } = await import('../src/chrome/src/cdp/cdp-client.js');
+
+ try {
+ // 1. Direct startDialogHandling timeout on cdpClient
+ const startup = cdpClient.startDialogHandling(71, { timeoutMs: 20 });
+ await assert.rejects(startup, error => error.code === 'dialog_startup_timeout');
+
+ // cleanupRun and detach must NOT hang awaiting the never-settling attach
+ let cleanupCompleted = false;
+ const cleanup = cdpClient.cleanupRun(71).then(() => { cleanupCompleted = true; });
+ await tick();
+ assert.equal(cleanupCompleted, true);
+ await cleanup;
+
+ // Late attach callback execution should immediately detach and not add session
+ if (attachCallback) {
+ attachCallback();
+ assert.equal(cdpClient.sessions.has(71), false);
+ }
+
+ // 2. Saved workflow replay timeout releases run entry and tab
+ const agent = new Agent({ getActive: () => ({ promptTier: 'full' }) });
+ agent._hydrate = async () => {};
+ const origStart = cdpClient.startDialogHandling.bind(cdpClient);
+ cdpClient.startDialogHandling = (tabId, opts = {}) => origStart(tabId, { ...opts, timeoutMs: 20 });
+ try {
+ const workflowRun = agent.replaySavedWorkflow(72, { id: 'test', steps: [{}] });
+ await assert.rejects(workflowRun, error => error.code === 'dialog_startup_timeout');
+ assert.equal(agent.isRunning(72), false);
+ assert.equal(cdpClient.sessions.has(72), false);
+ } finally {
+ cdpClient.startDialogHandling = origStart;
+ }
+ } finally {
+ cdpClient.stopDialogHandling(71);
+ cdpClient.stopDialogHandling(72);
+ globalThis.chrome = previousChrome;
+ }
+});
+
test('go_back authorizes beforeunload for the current page and cleans up on completion and failure', async () => {
const previousChrome = globalThis.chrome;
const previousBrowser = globalThis.browser;