Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(extension): release debugger when attach completes after connection close (#43025)

Yury Semikhatskyc2298adf81b9
diff --git a/packages/extension/src/relayConnection.ts b/packages/extension/src/relayConnection.ts
index 928820ef97..23da08a9f0 100644
--- a/packages/extension/src/relayConnection.ts
+++ b/packages/extension/src/relayConnection.ts
@@ -117,9 +117,7 @@ export class RelayConnection {
detachTab(tabId: number): void {
if (this._closed || !this._attachedTabs.has(tabId))
return;
- chrome.debugger.detach({ tabId }).catch(error => {
- debugLog('Error detaching tab:', error);
- });
+ detachDebugger(tabId);
this._notifyTabDetached(tabId);
this._sendMessage({
method: 'chrome.debugger.onDetach',
@@ -161,7 +159,7 @@ export class RelayConnection {
l.remove();
this._eventListeners = [];
for (const tabId of [...this._attachedTabs]) {
- chrome.debugger.detach({ tabId }).catch(() => {});
+ detachDebugger(tabId);
this._notifyTabDetached(tabId);
}
this.onclose?.();
@@ -286,8 +284,13 @@ export class RelayConnection {
// Attach bookkeeping; detach flows through the chrome.debugger.onDetach event.
if (message.method === 'chrome.debugger.attach') {
const target = args[0] as chrome.debugger.Debuggee | undefined;
- if (target?.tabId !== undefined)
- this._notifyTabAttached(target.tabId);
+ if (target?.tabId !== undefined) {
+ // The connection may have closed while the attach was pending.
+ if (this._closed)
+ detachDebugger(target.tabId);
+ else
+ this._notifyTabAttached(target.tabId);
+ }
}
return result ?? {};
}
@@ -324,6 +327,12 @@ function resolveChromeMember(fullMethod: string): { obj: any; name: string } {
return { obj, name: parts[parts.length - 1] };
}
+function detachDebugger(tabId: number): void {
+ chrome.debugger.detach({ tabId }).catch(error => {
+ debugLog('Error detaching tab:', error);
+ });
+}
+
async function invokeChromeMethod(fullMethod: string, args: any[]): Promise<any> {
const { obj, name } = resolveChromeMember(fullMethod);
const fn = obj[name] as (...a: any[]) => any;
diff --git a/tests/extension/tab-management.spec.ts b/tests/extension/tab-management.spec.ts
index 698baeb1a4..b3a3f2c08d 100644
--- a/tests/extension/tab-management.spec.ts
+++ b/tests/extension/tab-management.spec.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import { test, expect, connectAndNavigate } from './extension-fixtures';
+import { test, expect, connectAndNavigate, extensionId } from './extension-fixtures';
test(`browser_tabs new creates a new tab`, async ({ startExtensionClient, server }) => {
server.setContent('/second.html', '<title>Second</title><body>Second page<body>', 'text/html');
@@ -166,3 +166,48 @@ test(`window.open from tracked tab auto-attaches new tab`, async ({ startExtensi
result: expect.stringMatching(/- 0:.*\[Opener\].*\n- 1:.*\[Opened\]/),
});
});
+
+test(`pending attach is released when the connection closes`, {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43005' },
+}, async ({ startExtensionClient, server }) => {
+ server.setContent('/second.html', '<title>Second</title><body>Second page</body>', 'text/html');
+ const { browserContext, client } = await startExtensionClient();
+ await connectAndNavigate(browserContext, client, server.HELLO_WORLD);
+
+ const [sw] = browserContext.serviceWorkers();
+ await sw.evaluate(() => {
+ const g = globalThis as any;
+ const originalAttach = g.chrome.debugger.attach.bind(g.chrome.debugger);
+ g.__originalAttach = originalAttach;
+ g.chrome.debugger.attach = (target: any, version: string) => {
+ g.__heldTabId = target.tabId;
+ g.__pendingAttach = new Promise(resolve => g.__releaseAttach = resolve).then(() => originalAttach(target, version));
+ return g.__pendingAttach;
+ };
+ });
+
+ void client.callTool({ name: 'browser_tabs', arguments: { action: 'new', url: server.PREFIX + '/second.html' } }).catch(() => {});
+ await expect.poll(() => sw.evaluate(() => (globalThis as any).__heldTabId)).toBeTruthy();
+
+ await client.close();
+ const statusPage = await browserContext.newPage();
+ await statusPage.goto(`chrome-extension://${extensionId}/status.html`);
+ await expect(statusPage.locator('.client-info')).toHaveCount(0);
+
+ await sw.evaluate(async () => {
+ const g = globalThis as any;
+ g.__releaseAttach();
+ await g.__pendingAttach;
+ });
+ const isAttached = () => sw.evaluate(async () => {
+ const g = globalThis as any;
+ try {
+ await g.__originalAttach({ tabId: g.__heldTabId }, '1.3');
+ } catch {
+ return true;
+ }
+ await g.chrome.debugger.detach({ tabId: g.__heldTabId });
+ return false;
+ });
+ await expect.poll(isAttached).toBe(false);
+});