Skip to content

fix(cli): close in-flight browser launches when the session closes - #3486

Open
gshost1 wants to merge 1 commit into
browserbase:mainfrom
gshost1:fix/cli-close-during-init
Open

gshost1 wants to merge 1 commit into
browserbase:mainfrom
gshost1:fix/cli-close-during-init

Conversation

@gshost1

@gshost1 gshost1 commented Nov 18, 2026 •

Copy link
Copy Markdown

why

Fixes #3485.

If browse stop (or SIGTERM) closes the driver daemon while its initial browser launch is still in flight — for example after the browse open client disconnects via Ctrl+C or a request timeout — DriverSessionManager.close() sees this.stagehand === null and this.browser === null and returns immediately, allowing the daemon to unlink its socket and PID files. When the pending initialize() call then finishes, it unconditionally assigns this.browser, this.stagehand, and this.context, leaving the newly launched browser (or remote Browserbase session) open inside an unreachable daemon process.

what changed

  • packages/cli/src/lib/driver/session-manager.ts: track a closeGeneration counter incremented in close(). Capture the generation at the start of initialize(), and if close() ran before Stagehand.create() completed, close the Stagehand instance and throw so the existing catch block closes browser instead of installing it on the closed manager.
  • packages/cli/tests/driver-foundation.test.ts: add unit and real daemon-socket regression tests covering DriverSessionManager.close() and a stop request arriving over the daemon socket while open's browser launch is still pending.
  • .changeset/close-in-flight-browser-launch.md: add a browse patch changeset.

test plan

  • Both new regression tests fail on main (8901460d, closeStagehand / closeBrowser called 0 times) and pass with the fix.
  • End-to-end check on macOS with a spawned browse daemon process and headless Chrome: dropping an in-flight open client and sending stop over the unix socket previously left both daemon and headless Chrome alive after .sock/.pid removal; with this fix, Chrome closes and daemon exits with code 0.
  • pnpm --filter browse lint (format:check, oxlint, tsc --noEmit), pnpm --filter browse build, and the full packages/cli vitest suite (44 passed files, 589 passed tests) pass.

Summary by cubic

Fixes browse stop (or SIGTERM) leaving a browser running in an unreachable daemon when the close happens while the initial browser launch is still in flight.

  • DriverSessionManager now tracks a close generation, so an initialize() that finishes after close() tears down the newly created Stagehand and browser instead of installing them on the closed session.
  • Adds unit and daemon-socket regression tests and a browse patch changeset.

Written for commit c14322d. Summary will update on new commits.

View guided diff

Track close generations on DriverSessionManager so an initialize() call that finishes after close() tears down the newly created Stagehand and browser instead of installing them on a closed session.
@changeset-bot

changeset-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c14322d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
browse Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

Copy link
Copy Markdown
Contributor

This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run.
Approving the latest commit mirrors it into an internal PR owned by the approver.
If new commits are pushed later, the internal PR stays open but is marked stale until someone approves the latest external commit and refreshes it.

@github-actions github-actions Bot added external-contributor Tracks PRs mirrored from external contributor forks. external-contributor:awaiting-approval Waiting for a stagehand team member to approve the latest external commit. labels Nov 18, 2026

This branch has not been deployed

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

Labels

external-contributor:awaiting-approval Waiting for a stagehand team member to approve the latest external commit. external-contributor Tracks PRs mirrored from external contributor forks.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

browse stop during an in-flight open leaves the newly launched browser and daemon process running after removing .sock/.pid

1 participant