fix: kill leaked Chrome processes on profile seed failure When a profile is pinned and ProcessSingleton fails to delegate, the spawned Chrome process starts a full second browser instance. The sentinel never appeared, but the spawned process was never killed (detached + unref'd + reference discarded), accumulating zombie browsers that consume RAM. Three changes: - Store child process ref and expose kill() on ProfileWindowHandle. Call kill() when the sentinel doesn't appear within the timeout. - Only clear profileContextId when the browser UUID changes (Chrome mints a fresh UUID on every launch), avoiding unnecessary re-spawns. - Don't clear profileContextId in setProfilePin when the pin hasn't changed, preventing gratuitous re-spawns on session_start.
amankumarsingh77 amankumarsingh7702@gmail.com
Sun, 26 Jul 2026 00:33:44 +0530
5 files changed,
32 insertions(+),
10 deletions(-)
M
CHANGELOG.md
→
CHANGELOG.md
@@ -2,6 +2,14 @@ # Changelog
All notable changes to pi-browser-harness will be documented in this file. +## 0.10.1 — 2026-07-26 + +### Fixed + +- **Chrome process leak on profile seed failure.** When a profile is pinned and `seedProfileWindow` spawns Chrome via `openProfileWindow`, the spawned child is kept as `detached: true` + `unref()`. If Chromium's ProcessSingleton fails to delegate (user-data-dir mismatch, multiple browsers, Chrome busy/crashed), the spawned process starts a full second browser instance. The sentinel never appears, the seed times out, but the spawned Chrome process was never killed — accumulating zombie browser instances that consume RAM. The fix stores the child process reference, exposes a `kill()` on the returned handle, and calls it on seed timeout. +- **Unnecessary Chrome re-spawns on every `start()`.** `profileContextId` was unconditionally cleared on every `start()`, forcing a new profile-window launch via ProcessSingleton even when Chrome hadn't restarted and the context was still valid. Now the context is cleared only when the browser UUID in the WebSocket URL changes — which Chrome re-mints on every launch. +- **`setProfilePin` no longer clears context when the pin hasn't changed.** Loading the same pin from disk on `session_start` was clearing `profileContextId`, forcing yet another Chrome spawn on the next `start()`. + ## 0.10.0 — 2026-07-25 ### Added
M
package.json
→
package.json
@@ -1,6 +1,6 @@
{ "name": "pi-browser-harness", - "version": "0.10.0", + "version": "0.10.1", "description": "Browser control extension for pi — navigate, click, type, screenshot, and extract data from real Chrome via CDP", "keywords": [ "pi-package",
M
src/client.ts
→
src/client.ts
@@ -170,11 +170,12 @@ userDataDir = discovered.data.userDataDir;
} const connected = await transport.connect(wsUrl, { timeoutMs: 10_000 }); if (!connected.success) return connected; - if (!remote) { - remote = { cdpUrl: wsUrl, browserId: wsUrl.split("/").pop() ?? "unknown" }; + + const newBrowserId: string = wsUrl.split("/").pop() ?? "unknown"; + if (!remote || remote.browserId !== newBrowserId) { + profileContextId = undefined; } - // A fresh browser run invalidates any context id resolved earlier. - profileContextId = undefined; + remote = { cdpUrl: wsUrl, browserId: newBrowserId }; const seeded = await seedIfPinned(); if (!seeded.success) { await transport.close();@@ -397,9 +398,13 @@ transport: () => transport,
userDataDir: () => userDataDir, profilePin: () => profilePin, setProfilePin: (pin) => { + const changed = pin === null + ? profilePin !== null + : profilePin === null || profilePin.userDataDir !== pin.userDataDir || profilePin.profileDir !== pin.profileDir; profilePin = pin; - // The old profile's context no longer describes where tabs should go. - profileContextId = undefined; + if (changed) { + profileContextId = undefined; + } }, profileContextId: () => profileContextId, setProfileContextId: (contextId) => {
M
src/profile/bind.ts
→
src/profile/bind.ts
@@ -93,7 +93,7 @@ if (!launched.success) {
return err(cdpError("discovery_failed", `${launched.error} — ${manualFallbackHint(pin)}`)); } - const { sentinelUrl, cleanup } = launched.data; + const { sentinelUrl, cleanup, kill } = launched.data; const deadline = Date.now() + SEED_DEADLINE_MS; let seed: PageTarget | undefined; while (Date.now() < deadline && !seed) {@@ -102,6 +102,7 @@ seed = (await pageTargets(client)).find((t) => t.url === sentinelUrl);
} if (!seed) { + kill(); await cleanup(); return err(cdpError( "timeout",
M
src/profile/launch.ts
→
src/profile/launch.ts
@@ -15,7 +15,7 @@ * the sentinel URL — rather than diffing the target list — also keeps
* identification correct when the user opens tabs at the same moment. */ -import { spawn } from "node:child_process"; +import { spawn, type ChildProcess } from "node:child_process"; import { randomUUID } from "node:crypto"; import { rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os";@@ -42,6 +42,7 @@ /** file:// URL that will appear as the new window's page target. */
readonly sentinelUrl: string; /** Remove the sentinel file. Safe to call more than once. */ readonly cleanup: () => Promise<void>; + readonly kill: () => void; }; const SENTINEL_HTML = (token: string): string =>@@ -68,10 +69,11 @@ const args: string[] = [];
if (req.explicitUserDataDir) args.push(`--user-data-dir=${req.explicitUserDataDir}`); args.push(`--profile-directory=${req.profileDir}`, "--new-window", sentinelUrl); + let child: ChildProcess | null = null; try { // No shell: arguments are passed as an array, so profile names and paths // containing spaces survive intact on every platform. - const child = spawn(req.exePath, args, { detached: true, stdio: "ignore" }); + child = spawn(req.exePath, args, { detached: true, stdio: "ignore" }); child.unref(); // The launcher process exits as soon as the running browser takes over the // command line; an error here means the binary itself could not be started.@@ -85,6 +87,12 @@ return ok({
sentinelUrl, cleanup: async () => { await rm(file, { force: true }).catch(() => {}); + }, + kill: () => { + if (child) { + try { child.kill("SIGTERM"); } catch {} + child = null; + } }, }); };