Skip to content

fix(browser): close the agent's browser when the session that opened it ends - #492

Open
AmrDab wants to merge 2 commits into
mainfrom
fix/agent-browser-lifetime
Open

AmrDab wants to merge 2 commits into
mainfrom
fix/agent-browser-lifetime

Conversation

@AmrDab

@AmrDab AmrDab commented Nov 29, 2026

Copy link
Copy Markdown
Owner

What

The agent's browser (its own ~/.clawdcursor/cdp-profile instance on the agent CDP port, never the user's browser) now closes when the session that opened it ends. A later clawdcursor start also closes one whose session ended without a clean shutdown.

Why

cdp-driver.ts spawns the agent browser detached + unref() so that it survives the server, and nothing ever closed it.

  • macOS: one was still running 36 days after the session that opened it ended. Its parent was launchd. It used ~130 MB and 146 CPU-minutes over its life.
  • Windows: one had been open for 2 days, with 16 processes and ~740 MB.

How

New src/platform/agent-browser-life.ts:

  • Launch: only the server that launches the browser writes ~/.clawdcursor/agent-browser.json, containing {ownerPid, ownerStarted, port, browserPid, browserStarted}. It uses temp + rename. A server that only connects to a running agent browser owns nothing.
  • Owner shutdown: through the existing single exitRoutine in serve.ts (SIGINT, SIGTERM, stdin end/close/error, the parent-pid watchdog, HTTP /stop). The owner sends CDP Browser.close over its own connection, bounded to 2 s in total, then deletes the record.
  • Start-up sweep: deferred, off the startup path. If the record's owner is gone (pid dead, or reused with another start time), the browser is closed only when the recorded browser pid is still that exact process (pid plus start time). Otherwise nothing is closed. The record is deleted either way.
    • A live owner, including another session's, is left alone.
    • An alive owner whose start time can't be read counts as alive.
  • Identity: it doesn't rely on Browser.getBrowserCommandLine, which Chromium answers only with --enable-automation.
  • Fence: blocks closing as it blocks launching (bench isolation).
  • Another session on the same browser: its next browser call relaunches through the existing connect-or-launch path. No change was needed there, and a test covers it.

On Windows, a host that terminates the server outright runs no handler. Then the next clawdcursor start cleans up.

Tests

src/__tests__/agent-browser-life.test.ts has 12 cases:

  • Recording: launch writes the record with the spawned pid; connect-only doesn't.
  • Owner shutdown: the owner's shutdown closes the browser and deletes the record; a non-owner does nothing; a replaced record does nothing.
  • Sweep, owner gone: a dead owner with a live browser pid of the same start time is closed.
  • Sweep, browser not ours: a dead browser pid, or a reused one, is not closed.
  • Sweep, owner alive: a live owner is left alone; so is a reused owner pid.
  • Fence: blocks the sweep.
  • Relaunch: after the owner closed the browser, another server relaunches it.

typecheck, typecheck:tests, lint (0 errors, the 3 warnings as on main), npm run test:ci: 4309 passed, 80 skipped, 0 failed.

Note for #482 (playwright-core): the new file imports chromium from playwright like cdp-driver.ts, so whichever merges second updates that import.

No release: for the 2.4 train.

🤖 Generated with Claude Code

AmrDab and others added 2 commits November 28, 2026 20:38
…it ends

The server that launches the agent browser records itself as owner in
~/.clawdcursor/agent-browser.json and closes the browser (CDP Browser.close,
bounded to 2 s) in the shared exit routine. A server that only connected owns
nothing. At start, in the background, a record whose owner is gone (dead pid,
or a reused pid with another start time) gets its browser closed, but only if
the browser's command line shows the clawdcursor cdp-profile. The test fence
blocks closing as it blocks launching.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Chromium answers Browser.getBrowserCommandLine only when started with
--enable-automation, which the agent browser is not, so the start-up sweep
never closed anything. The launch record now holds the spawned browser's pid
and start time; the sweep closes only a browser whose pid is alive with that
start time. A gone or reused pid is not closed and the record is dropped.
The owner's shutdown close is bounded to 2 s in total.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant