fix(perf): bound browser/context close() with a timeout in the cold-load A/B harness

Merged
PE
3mo ago
+178-13

Refs #4116. This fixes the harness-level symptom (the browser-close hang is now bounded and reported instead of hanging indefinitely), but it does not perform the fixture-size root-cause investigation #4116 asked for — see the Scope note below.

scripts/perf/browser-cold-ab.mts's teardown did await x.close().catch(...) for both the Playwright BrowserContext and Browser. That only handles a rejection — an unresolved promise is neither resolved nor rejected, so when close() never settles the await blocks forever with nothing recorded. #4116 reports exactly this: the 1.26 GB fixture hung on browser close in both arms of two independent, unrelated experiments (#4054, #4055), which is the signature of a harness defect rather than anything candidate-specific — the harness code is shared regardless of which build is being measured.

Fix

New scripts/perf/browser-cold-teardown.ts exports closeContextWithTimeout / closeBrowserWithTimeout, which race each close() call against a deadline (new --close-timeout-ms flag, default 30s) and, on expiry, return a named error string instead of hanging. The harness records that error into the sample's JSON/JSONL output exactly as it already did for a rejection, so a stuck close now becomes a diagnosable failure rather than an indefinite hang.

Playwright's public Browser/BrowserContext API returned by chromium.launch() does not expose a handle to the underlying OS process, so this cannot also force-kill a stuck browser process. The harness's own final process.exit() still terminates the run itself even if a browser process is left behind.

Reproduction

The 1.26 GB fixture is disproportionate to fetch for iteration (checked: df -h / showed ~30 GiB free beforehand), so this was verified with a small, direct reproduction of the actual mechanism instead of the full fixture:

  • Before (the bug): a fake close() that never settles, run through the exact pre-fix pattern (await x.close().catch(...)) — confirmed hanging past an 8s external watchdog, i.e. it does not resolve on its own.
  • After (the fix), against the real shipped browser-cold-teardown.ts:
    closeContextWithTimeout returned after 1501ms: context.close() did not resolve within 1500ms
    closeBrowserWithTimeout returned after 1501ms: browser.close() did not resolve within 1500ms
    closeBrowserWithTimeout (healthy) returned after 0ms: null
    
    A hanging close is now bounded and loudly reported; a healthy close still returns immediately with no error.

Mutation

Reverted closeContextWithTimeout to the pre-fix bare-await pattern and reran the same hanging-close case:

MUTATION CONFIRMED: closeContextWithTimeout is still hanging after 6000ms (root cause reproduced)

Confirms the fix is load-bearing. Restored the fix afterward (verified byte-identical to the committed version via diff).

Gates (repo root)

  • node scripts/check-module-size.mjs → OK, 0 new over budget
  • node scripts/check-test-wiring.mjs → OK
  • node scripts/check-source-text-assertions.mjs → OK, 0 new

Scope note

Full-fixture-size confirmation (the actual 1.26 GB model against a real browser hang) was not attempted — fetching and running that repeatedly was judged disproportionate for verifying a harness-teardown fix whose mechanism (an unresolved close() promise) is fully reproducible at small scale. The fix addresses the mechanism described in the issue; it has not been proven to eliminate every possible cause of a slow close() at that fixture size, only to guarantee the harness never hangs on one indefinitely and always reports what it was waiting on.

No changeset: this only touches an internal scripts/perf/ harness, not a published package.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Browser performance tests no longer hang indefinitely when browser or context cleanup fails to complete.
    • Cleanup timeouts are now reported as named failures instead of blocking test execution.
  • New Features

    • Added a configurable cleanup timeout for browser performance tests, defaulting to 30 seconds.