Skip to content

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

fix(chromium): dispose worker sessions when their frame session is disposed (#42282)

Yury Semikhatskycf107fca92ed
diff --git a/packages/playwright-core/src/server/chromium/crPage.ts b/packages/playwright-core/src/server/chromium/crPage.ts
index 52aab753da..f53e0ecec6 100644
--- a/packages/playwright-core/src/server/chromium/crPage.ts
+++ b/packages/playwright-core/src/server/chromium/crPage.ts
@@ -580,6 +580,8 @@ class FrameSession {
this._firstNonInitialNavigationCommittedReject(new TargetClosedError(this._page.closeReason()));
for (const childSession of this._childSessions)
childSession.dispose();
+ for (const sessionId of this._workerSessions.keys())
+ this._removeWorkerSession(sessionId);
if (this._parentSession)
this._parentSession._childSessions.delete(this);
eventsHelper.removeEventListeners(this._eventListeners);
@@ -780,16 +782,21 @@ class FrameSession {
session.on('Runtime.exceptionThrown', exception => this._page.addPageError(exceptionToError(exception.exceptionDetails), stackTraceToLocation(exception.exceptionDetails.stackTrace)));
}
+ private _removeWorkerSession(sessionId: string): boolean {
+ const workerSession = this._workerSessions.get(sessionId);
+ if (!workerSession)
+ return false;
+ this._workerSessions.delete(sessionId);
+ this._crPage._networkManager.removeSession(workerSession);
+ workerSession.dispose();
+ this._page.removeWorker(sessionId);
+ return true;
+ }
+
_onDetachedFromTarget(event: Protocol.Target.detachedFromTargetPayload) {
// This might be a worker...
- const workerSession = this._workerSessions.get(event.sessionId);
- if (workerSession) {
- this._workerSessions.delete(event.sessionId);
- this._crPage._networkManager.removeSession(workerSession);
- workerSession.dispose();
- this._page.removeWorker(event.sessionId);
+ if (this._removeWorkerSession(event.sessionId))
return;
- }
// ... or an oopif.
const childFrameSession = this._crPage._sessions.get(event.targetId!);
diff --git a/tests/library/chromium/oopif.spec.ts b/tests/library/chromium/oopif.spec.ts
index a8971fe638..2a9f525472 100644
--- a/tests/library/chromium/oopif.spec.ts
+++ b/tests/library/chromium/oopif.spec.ts
@@ -15,6 +15,7 @@
*/
import { contextTest as it, expect } from '../../config/browserTest';
+import { attachFrame } from '../../config/utils';
import type { Frame, Browser } from '@playwright/test';
it.use({
@@ -43,6 +44,34 @@ it('should handle oopif detach', async function({ page, browser, server }) {
expect(detachedFrame).toBe(frame);
});
+it('should remove workers of a detached oopif', async function({ page, browser, server }) {
+ await page.goto(server.EMPTY_PAGE);
+ const [worker] = await Promise.all([
+ page.waitForEvent('worker'),
+ attachFrame(page, 'frame1', server.CROSS_PROCESS_PREFIX + '/worker/worker.html'),
+ ]);
+ await assertOOPIFCount(browser, 1);
+ expect(page.workers().length).toBe(1);
+ await Promise.all([
+ worker.waitForEvent('close'),
+ page.goto(server.PREFIX + '/title.html'),
+ ]);
+ expect(page.workers().length).toBe(0);
+});
+
+it('should not hang in unrouteAll when oopif worker is gone', async function({ page, context, browser, server }) {
+ it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/42278' });
+ await context.route('**/*', route => route.continue());
+ await page.goto(server.EMPTY_PAGE);
+ await Promise.all([
+ page.waitForEvent('worker'),
+ attachFrame(page, 'frame1', server.CROSS_PROCESS_PREFIX + '/worker/worker.html'),
+ ]);
+ await assertOOPIFCount(browser, 1);
+ await page.goto(server.CROSS_PROCESS_PREFIX + '/title.html');
+ await context.unrouteAll();
+});
+
it('should handle remote -> local -> remote transitions', async function({ page, browser, server }) {
await page.goto(server.PREFIX + '/dynamic-oopif.html');
expect(page.frames().length).toBe(2);