Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore: stop tracing upon client disconnect (#36656)
Dmitry Gozmane67c06c12955
diff --git a/packages/playwright-core/src/server/dispatchers/dispatcher.ts b/packages/playwright-core/src/server/dispatchers/dispatcher.tsindex b868690877..a6fe1c828c 100644--- a/packages/playwright-core/src/server/dispatchers/dispatcher.ts+++ b/packages/playwright-core/src/server/dispatchers/dispatcher.ts@@ -372,6 +372,9 @@ export class DispatcherConnection {await sdkObject.instrumentation.onBeforeCall(sdkObject, callMetadata);const response: any = { id };try {+ // If the dispatcher has been disposed while running the instrumentation call, error out.+ if (this._dispatcherByGuid.get(guid) !== dispatcher)+ throw new TargetClosedError(closeReason(sdkObject));const result = await dispatcher._runCommand(callMetadata, method, validParams);const validator = findValidator(dispatcher._type, method, 'Result');response.result = validator(result, '', this._validatorToWireContext());diff --git a/packages/playwright-core/src/server/dispatchers/tracingDispatcher.ts b/packages/playwright-core/src/server/dispatchers/tracingDispatcher.tsindex 2e295246d6..678d35b1e9 100644--- a/packages/playwright-core/src/server/dispatchers/tracingDispatcher.ts+++ b/packages/playwright-core/src/server/dispatchers/tracingDispatcher.ts@@ -25,6 +25,7 @@ import type { Progress } from '@protocol/progress';export class TracingDispatcher extends Dispatcher<Tracing, channels.TracingChannel, BrowserContextDispatcher | APIRequestContextDispatcher> implements channels.TracingChannel {_type_Tracing = true;+ private _started = false;static from(scope: BrowserContextDispatcher | APIRequestContextDispatcher, tracing: Tracing): TracingDispatcher {const result = scope.connection.existingDispatcher<TracingDispatcher>(tracing);@@ -37,6 +38,7 @@ export class TracingDispatcher extends Dispatcher<Tracing, channels.TracingChannasync tracingStart(params: channels.TracingTracingStartParams, progress: Progress): Promise<channels.TracingTracingStartResult> {this._object.start(params);+ this._started = true;}async tracingStartChunk(params: channels.TracingTracingStartChunkParams, progress: Progress): Promise<channels.TracingTracingStartChunkResult> {@@ -58,7 +60,14 @@ export class TracingDispatcher extends Dispatcher<Tracing, channels.TracingChann}async tracingStop(params: channels.TracingTracingStopParams, progress: Progress): Promise<channels.TracingTracingStopResult> {+ this._started = false;await this._object.stop(progress);}+ override _onDispose() {+ // Avoid protocol calls for the closed context.+ if (this._started)+ this._object.stopChunk(undefined, { mode: 'discard' }).then(() => this._object.stop(undefined)).catch(() => {});+ this._started = false;+ }}diff --git a/packages/playwright-core/src/server/trace/recorder/tracing.ts b/packages/playwright-core/src/server/trace/recorder/tracing.tsindex c23e665dfb..37148ddb44 100644--- a/packages/playwright-core/src/server/trace/recorder/tracing.ts+++ b/packages/playwright-core/src/server/trace/recorder/tracing.ts@@ -299,7 +299,7 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snapsstate.networkFile = newNetworkFile;}- async stop(progress: Progress) {+ async stop(progress: Progress | undefined) {if (!this._state)return;if (this._isStopping)@@ -309,7 +309,8 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snapsthis._closeAllGroups();this._harTracer.stop();this.flushHarEntries();- await progress.race(this._fs.syncAndGetError()).finally(() => {+ const promise = progress ? progress.race(this._fs.syncAndGetError()) : this._fs.syncAndGetError();+ await promise.finally(() => {this._state = undefined;});}@@ -341,7 +342,7 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snapsthis.groupEnd();}- async stopChunk(progress: Progress, params: TracingTracingStopChunkParams): Promise<{ artifact?: Artifact, entries?: NameValue[] }> {+ async stopChunk(progress: Progress | undefined, params: TracingTracingStopChunkParams): Promise<{ artifact?: Artifact, entries?: NameValue[] }> {if (this._isStopping)throw new Error(`Tracing is already stopping`);this._isStopping = true;@@ -395,7 +396,8 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snapsthis._fs.zip(entries, zipFileName);// Make sure all file operations complete.- const error = await progress.race(this._fs.syncAndGetError()).catch(e => e);+ const promise = progress ? progress.race(this._fs.syncAndGetError()) : this._fs.syncAndGetError();+ const error = await promise.catch(e => e);this._isStopping = false;if (this._state)diff --git a/tests/library/multiclient.spec.ts b/tests/library/multiclient.spec.tsindex ee5e45d749..e5896693b2 100644--- a/tests/library/multiclient.spec.ts+++ b/tests/library/multiclient.spec.ts@@ -30,12 +30,13 @@ const test = playwrightTest.extend<ExtraFixtures>({await server.close();},connect: async ({ browserType }, use) => {- let browser: Browser | undefined;+ const browsers: Browser[] = [];await use(async (wsEndpoint, options = {}) => {- browser = await browserType.connect(wsEndpoint, options);+ const browser = await browserType.connect(wsEndpoint, options);+ browsers.push(browser);return browser;});- await browser?.close();+ await Promise.all(browsers.map(b => b.close()));},twoPages: async ({ remoteServer, connect }, use) => {const browserA = await connect(remoteServer.wsEndpoint());@@ -366,3 +367,17 @@ test('should avoid side effects upon disconnect', async ({ twoPages, server }) =expect(error.message).toContain(kTargetClosedErrorMessage);expect(counter).toBe(savedCounter);});++test('should stop tracing upon disconnect', async ({ twoPages, trace }) => {+ test.skip(trace === 'on');++ const { pageA, pageB } = twoPages;++ await pageA.context().tracing.start();+ const error = await pageB.context().tracing.start().catch(e => e);+ expect(error.message).toContain('Tracing has been already started');++ await disconnect(pageA);++ await pageB.context().tracing.start();+});