From 693b5e86268421b5c6e3400d988efdcc25f2e34e Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Sun, 26 Jul 2026 00:33:44 +0530 Subject: 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. --- src/client.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src/client.ts') diff --git a/src/client.ts b/src/client.ts index 1049212..4d8c3a8 100644 --- a/src/client.ts +++ b/src/client.ts @@ -170,11 +170,12 @@ export const createBrowserClient = (opts: BrowserClientOptions): BrowserClient = } 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 @@ export const createBrowserClient = (opts: BrowserClientOptions): BrowserClient = 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) => { -- cgit v1.2.3