Skip to content

fix(browser): close the browser when its page cannot be opened - #587

Merged
mrgoonie merged 4 commits into
mainfrom
fix/579-driver-newpage-failure
Oct 7, 2026
Merged

mrgoonie merged 4 commits into
mainfrom
fix/579-driver-newpage-failure

Conversation

@mrgoonie

@mrgoonie mrgoonie commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #579. Refs #564 Depends on #566. This branch is stacked on fix/564-driver-close-during-launch; the diff against main includes #566's commits until it lands. Rebase/merge main once #566 merges. ## Problem In packs/browser-playwright/src/driver.ts (#launch, reached from #ensurePage), context.newPage() ran outside the try that closes a failed start. If it threw, the persistent context stayed open holding the profile folder, #context still pointed at it, and the next call launched a second Chromium on the same profile. ## Fix Opening the page (context.pages() / context.newPage()) now sits inside the existing cleanup try, so a failure resets #context and closes the browser it started. #launching is already cleared by the finally in #ensurePage, so the next call starts cleanly; the closed/launching state machine from #566 is unchanged. ## Test New packs/browser-playwright/test/new-page-failure.spec.ts launches real Chromium (same pattern as close-during-launch.spec.ts), makes the first start report no pages and a failing newPage, and checks: - the first observe() rejects and the browser it started is disconnected; - the next observe() succeeds on a second launch, with exactly one browser still connected. Against the unfixed driver it fails (launched[0].browser().isConnected() is true). ## Verification - vitest run packs/browser-playwright apps/runtime/test/{browser-effects,session-preview-real,task-browser}.spec.ts: 9 files, 71 tests passed - pnpm typecheck, pnpm invariants, eslint on changed files: pass - pnpm verify: invariants/typecheck/lint pass; the full test run was on a machine at 100% CPU with other sessions' Chromium processes, and two runs failed different, unrelated timing/EPERM specs (worktree-sweep, managed-worktree, scan-secret-history, package-fetch, scoped-fs, and once the browser pack's 20s timeouts). Every failing file passed when rerun on its own; in the second full run all six browser-pack files passed. No user-visible behavior, API or docs change.

…er closed

close() returned at once while Chromium was launching, so the browser that
finished starting afterwards kept running and held its profile folder. The
driver now keeps the in-flight start, shares it between callers, and close()
waits for it; a start that finishes after a close shuts its browser before
returning, and a closed driver starts nothing again.
A newPage that threw after Chromium was up left the persistent context open and holding the profile, so the next call started a second browser on the same profile folder. Opening the page now sits inside the cleanup that closes a failed start.

Refs #579, #564
@mrgoonie

mrgoonie commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Review attestation: ready to merge at 08be431433ede86f781e009ee88d5e3f38fd35c7, reviewed by agent:code-reviewer.

A push to this PR makes this attestation stale; the new head needs its own review.

@mrgoonie
mrgoonie enabled auto-merge (squash) October 7, 2026 20:03
@mrgoonie
mrgoonie merged commit 7ed5ab9 into main Oct 7, 2026
56 of 60 checks passed
@mrgoonie
mrgoonie deleted the fix/579-driver-newpage-failure branch October 7, 2026 21:31
@mrgoonie

mrgoonie commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Heads-up from #622 (#563): packs/browser-playwright/test/new-page-failure.spec.ts calls rmSync(dir, { ..., maxRetries: 10, retryDelay: 200 }). On Windows, under Node 24, that form fails with EPERM at once and never runs its retries. #622 adds the invariant test-cleanup-retries-asynchronously, which fails on that call. Whichever PR lands second should change it to await removeTestDirectory(dir) from tools/test-cleanup.ts, in an async afterAll.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(browser): a failed newPage leaves the context open and the next call starts a second browser

1 participant