{"record":{"id":"eebcc9dd0394bd3f","repo":"microsoft/playwright","slug":"tracing-has-been-already-started","errorCode":null,"errorMessage":"Tracing has been already started","messagePattern":"Tracing has been already started","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/playwright-core/src/server/trace/recorder/tracing.ts","lineNumber":154,"sourceCode":"  }\n\n  private _sdkLanguage() {\n    return this._context instanceof BrowserContext ? this._context._browser.sdkLanguage() : this._context.attribution.playwright.options.sdkLanguage;\n  }\n\n  async resetForReuse(progress: Progress) {\n    // Discard previous chunk if any and ignore any errors there.\n    await this.stopChunk(progress, { mode: 'discard' }).catch(() => {});\n    await progress.race(this._stop());\n    if (this._snapshotter)\n      await progress.race(this._snapshotter.resetForReuse());\n  }\n\n  start(progress: Progress, options: TracerOptions) {\n    if (this._isStopping)\n      throw new Error('Cannot start tracing while stopping');\n    if (this._state)\n      throw new Error('Tracing has been already started');\n\n    // Re-write for testing.\n    this._contextCreatedEvent.sdkLanguage = this._sdkLanguage();\n\n    // TODO: passing the same name for two contexts makes them write into a single file\n    // and conflict.\n    const traceName = options.name || createGuid();\n\n    const tracesDir = this._createTracesDirIfNeeded();\n\n    // Init the state synchronously.\n    this._state = {\n      options,\n      traceName,\n      tracesDir,\n      traceFile: path.join(tracesDir, traceName + '.trace'),\n      networkFile: path.join(tracesDir, traceName + '.network'),\n      chunkOrdinal: 0,","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/playwright-core/src/server/trace/recorder/tracing.ts#L136-L172","documentation":"Tracing.start() throws when an active trace already exists (this._state is set). Tracing is single-instance per context: you must fully stop an existing trace before starting a new one. This protects against overlapping trace writers that would share the same trace/network files.","triggerScenarios":"Calling context.tracing.start(...) twice in a row without context.tracing.stop() between them; calling start when the test runner already enabled tracing for the project.","commonSituations":"A beforeEach hook enables tracing and the test body also calls tracing.start; two suites sharing a browser context both try to own tracing; forgetting to call tracing.stop() at the end of a previous run.","solutions":["Guard with tracing.stop() before start, or check that tracing isn't already started.","Remove the duplicate start call — let only the test runner's trace config own tracing.","Use a fresh browser context per trace-owned test so _state cannot carry over."],"exampleFix":"// before\nawait context.tracing.start({ snapshots: true });\nawait context.tracing.start({ screenshots: true }); // already started\n\n// after\nawait context.tracing.start({ snapshots: true, screenshots: true });\n// ... or stop first:\nawait context.tracing.stop();\nawait context.tracing.start({ screenshots: true });","handlingStrategy":"validation","validationCode":"async function startOnce(ctx, opts) {\n  try {\n    await ctx.tracing.start(opts);\n  } catch (e) {\n    if (!/already been started/i.test(String(e.message))) throw e;\n    // already started; optionally stop and restart with new options\n    await ctx.tracing.stop();\n    await ctx.tracing.start(opts);\n  }\n}","typeGuard":null,"tryCatchPattern":"try { await context.tracing.start(opts); }\ncatch (e) {\n  if (/already been started/i.test(String(e.message))) {\n    await context.tracing.stop();\n    await context.tracing.start(opts);\n  } else throw e;\n}","preventionTips":["Centralise tracing.start in one place (runner config or a single beforeAll).","Always pair start with a stop in a finally block.","Use a fresh context per test that needs distinct tracing."],"tags":["tracing","state-machine"],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}