Skip to content

[Feature]: Test runner captures and source-maps stacks that it never uses #44334

Done

Description

@tycrimm

Problem

Two hot paths in the worker format stacks that nobody reads:

  1. Built-in fixture steps (page, context, request, and about 30 more) have no user location. In fixtureRunner.ts, _stepInfo.stack is undefined, so testInfo._addStep() captures and source-maps a full raw stack, then filters it to 0 frames. This happens about 60 times per test.
  2. LongStandingScope._race() in packages/isomorphic/manualPromise.ts reads new Error().stack on every call. The formatted stack is used only when the scope closes during the race.

Measurements

CPU profile of a 24-test suite, Apple Silicon, Chromium headless shell, 1 worker, median of 5 runs:

Now With both changes
Worker CPU time 995 ms 806 ms (−19%)

Proposed approach

  1. Give built-in fixture steps stack: []. User fixtures keep their registration location.
  2. In _race(), keep the Error and read .stack only when it clones a close error. V8 records the frames when the Error is constructed, so close messages stay the same.

One difference: built-in fixture steps no longer report user locations injected by a custom Error.prepareStackTrace formatter. With the default formatter, they never had a location.

Already implemented and tested in #44326, which was closed only to file this issue first. Both are small, low-risk performance fixes. Reopening #44326 is the fastest path; it is ready for review.

Activity

  1. microsoft-playwright-automation commented on Nov 24, 2026

    @microsoft-playwright-automation
  2. microsoft-playwright-automation commented on Nov 24, 2026

    @microsoft-playwright-automation
    Contributor

    Hi, I'm the Playwright bot. I checked both stack paths.

    Reproduced on macOS at d70f422f491cfb875dd5dc05f01c61207e7382b4 (1.66.0-next). Built-in fixture steps fall back to capturing a stack despite having no user location; scope races format their captured stack even when the promise succeeds. The proposal supplies empty fixture stacks and reads the scope stack only when creating a close error. User fixture locations and the close error's original caller remain intact.

    Custom Error.prepareStackTrace hooks will no longer run for built-in fixture steps; for scope errors, they run at close rather than when the race starts. I verified the removed formatting work, not the reported 19% CPU reduction.

    Regression checks

    The existing should report api step hierarchy test now counts formatter calls whose frames include both _addStep and Fixture. Its zero-call assertion fails without the fixture change and passes with it; the existing step-location expectations still pass.

    The new MP-001 should format scope race stacks only on close unit test checks the scope directly. Its central check is:

    const prepareStackTrace = Error.prepareStackTrace;
    let stackFormats = 0;
    Error.prepareStackTrace = (error, frames) => {
      if (frames.some(frame => frame.getFunctionName() === '_race'))
        ++stackFormats;
      return prepareStackTrace(error, frames);
    };
    try {
      const scope = new iso.LongStandingScope();
      expect(await scope.race(Promise.resolve('done'))).toBe('done');
      expect(stackFormats).toBe(0);
    
      function startRace() {
        return scope.race(new iso.ManualPromise()).catch(error => error);
      }
      const result = startRace();
      expect(stackFormats).toBe(0);
      scope.close(new TypeError('scope closed'));
      const error = await result;
      expect(stackFormats).toBe(1);
      expect(error.name).toBe('TypeError');
      expect(error.message).toBe('scope closed');
      expect(error.stack).toContain('startRace');
    } finally {
      Error.prepareStackTrace = prepareStackTrace;
    }

    Here iso is imported from the repository's playwright-core/lib/coreBundle. Without the change, a successful scope race formats its stack once. With the change, it formats none; closing the pending race formats one and preserves its caller.

    What I ran
    • Installed the pinned dependencies with npm ci --no-audit --no-fund, then installed Chromium, Firefox, and WebKit. Initial setup checks failed only because dependencies and browser executables were absent.
    • npm run build and npm run flint: pass. The new test has its required identifier and test-registry entry.
    • npm run ttest -- test-step.spec.ts fixtures.spec.ts reporter-json.spec.ts reporter-list.spec.ts reporter-line.spec.ts --workers=4: 172 passed.
    • npm run test -- tests/library/unit/manual-promise.spec.ts tests/library/page-close.spec.ts tests/library/browsercontext-basic.spec.ts tests/page/workers.spec.ts --workers=4 --retries=0 --reporter=dot: 224 passed, 7 existing skips across Chromium, Firefox, and WebKit.
    • Rebuilt with the two source files restored to the exact base: the fixture regression failed and the scope regression failed in all three browser projects. Restored the fix, rebuilt, and reran both: the fixture check and all three scope checks passed.

    These are Node.js stack checks; no browser-specific difference was found. The linked #44326 is closed and unmerged, and has no regression test. No open team PR linking this issue appeared during the final check.

    Draft PR: #44338

    Triaged on macOS by the Playwright bot - agent run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-triage-done

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions