Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(browser): a kept Windows Chrome is listed again once its launcher shell is gone

aivsomkar69e3d34fc083
diff --git a/server/browser-processes.test.ts b/server/browser-processes.test.ts
index a874db7c3a..aecb24a239 100644
--- a/server/browser-processes.test.ts
+++ b/server/browser-processes.test.ts
@@ -387,7 +387,7 @@ describe("a browser on Windows", () => {
}
});
- it("keeps a daemon's own Chrome that the launcher batch file runs under cmd.exe, and stops it once that daemon is gone", async () => {
+ it("keeps a daemon's own Chrome that the launcher batch file runs under cmd.exe, and stops it once that shell or daemon is gone", async () => {
const root = mkdtempSync(join(tmpdir(), "omb-win-launcher-"));
homes.push(root);
const dir = join(root, "Kept Profile");
@@ -396,6 +396,7 @@ describe("a browser on Windows", () => {
const started = "2026-11-03T09:00:00.0000000Z";
const browser = process.pid;
let daemonRuns = true;
+ let shellRuns = true;
const commands: string[][] = [];
vi.mocked(execFile).mockImplementation(((file: string, args: string[], _options: unknown, done: (error: Error | null, stdout: string) => void) => {
commands.push([file, ...args]);
@@ -404,12 +405,13 @@ describe("a browser on Windows", () => {
else if (file === "powershell.exe") {
stdout = JSON.stringify([{
ProcessId: browser, ParentProcessId: 4100, CommandLine: `"C:\\Chrome\\chrome.exe" "--user-data-dir=${dir}" --remote-debugging-port=0`,
- Name: "chrome.exe", Started: started, ParentName: "cmd.exe", ParentStarted: "2026-11-03T08:59:59.5000000Z",
+ Name: "chrome.exe", Started: started, ParentName: shellRuns ? "cmd.exe" : null, ParentStarted: shellRuns ? "2026-11-03T08:59:59.5000000Z" : null,
GrandparentId: daemonRuns ? 4000 : null, GrandparentName: daemonRuns ? "agent-browser.exe" : null,
GrandparentStarted: daemonRuns ? "2026-11-03T08:59:59.0000000Z" : null,
}]);
} else if (file === "tasklist") {
- stdout = daemonRuns && args[1] === "PID eq 4000" ? `"agent-browser.exe","4000","Console","1","12,345 K"\r\n` : "INFO: No tasks are running which match the specified criteria.\r\n";
+ const image = daemonRuns && args[1] === "PID eq 4000" ? "agent-browser.exe" : shellRuns && args[1] === "PID eq 4100" ? "cmd.exe" : null;
+ stdout = image ? `"${image}","${args[1].slice(7)}","Console","1","12,345 K"\r\n` : "INFO: No tasks are running which match the specified criteria.\r\n";
} else if (file === "taskkill") rmSync(join(dir, "lockfile"), { recursive: true, force: true });
queueMicrotask(() => done(null, stdout));
return {} as ChildProcess;
@@ -422,10 +424,17 @@ describe("a browser on Windows", () => {
expect(killed(), "a live daemon's Chrome is never stopped").toEqual([]);
await releaseBrowserProfile(dir, engine, launcher);
expect(commands.filter(([file]) => file === "powershell.exe"), "the kept Chrome is not listed again each turn").toHaveLength(1);
- daemonRuns = false;
+ // agent-browser stopped only the shell: its Chrome still holds the profile under a daemon that runs.
+ shellRuns = false;
await releaseBrowserProfile(dir, engine, launcher);
expect(killed()).toEqual([["taskkill", "/PID", String(browser), "/T", "/F"]]);
expect(existsSync(join(dir, "lockfile"))).toBe(false);
+ mkdirSync(join(dir, "lockfile"));
+ shellRuns = true;
+ daemonRuns = false;
+ await releaseBrowserProfile(dir, engine, launcher);
+ expect(killed()).toHaveLength(2);
+ expect(existsSync(join(dir, "lockfile"))).toBe(false);
});
it.runIf(process.platform === "win32")("lists a real browser under cmd.exe with its daemon, keeps it for that daemon and stops it for another", async () => {
@@ -442,9 +451,11 @@ describe("a browser on Windows", () => {
await vi.waitFor(() => expect(existsSync(ready)).toBe(true), { timeout: 15_000 });
const killed = () => vi.mocked(execFile).mock.calls.filter(([file]) => file === "taskkill");
vi.mocked(execFile).mockClear();
+ const listed = () => vi.mocked(execFile).mock.calls.filter(([file, args]) => file === "powershell.exe" && String((args as string[]).at(-1)).includes("OMB_BROWSER_PROFILE"));
+ await releaseBrowserProfile(dir, process.execPath, process.execPath);
await releaseBrowserProfile(dir, process.execPath, process.execPath);
- expect(vi.mocked(execFile).mock.calls.some(([file]) => file === "powershell.exe"), "the profile's browsers were listed").toBe(true);
expect(killed(), "its daemon's browser is kept").toEqual([]);
+ expect(listed(), "the second turn finds the kept browser without listing again").toHaveLength(1);
rmSync(join(dir, "lockfile"), { recursive: true, force: true });
await releaseBrowserProfile(dir);
mkdirSync(join(dir, "lockfile"));
@@ -452,7 +463,7 @@ describe("a browser on Windows", () => {
expect(killed()).toHaveLength(1);
await Promise.race([exited, new Promise((_, reject) => setTimeout(() => reject(new Error("the stopped browser's shell still runs")), 10_000))]);
} finally {
- shell.kill();
+ try { execFileSync("taskkill", ["/PID", String(shell.pid), "/T", "/F"], { stdio: "ignore" }); } catch {}
}
}, 60_000);
});
diff --git a/server/browser-processes.ts b/server/browser-processes.ts
index 8d16580b31..080e6f59f9 100644
--- a/server/browser-processes.ts
+++ b/server/browser-processes.ts
@@ -250,7 +250,7 @@ function lockReleased(lock: string): boolean {
catch (error) { return (error as NodeJS.ErrnoException).code === "ENOENT"; }
}
-const heldWindowsProfiles = new Map<string, { browser: number; daemon: number; image: string }>();
+const heldWindowsProfiles = new Map<string, { browser: number; daemon: number; image: string; shell?: number }>();
/** Windows Chrome keeps `lockfile` in its profile open and undeletable while it runs. One whose daemon was killed keeps running
* and makes every later launch on the profile exit with code 21 (profile in use), even after the app restarts, so it is
@@ -261,7 +261,7 @@ async function releaseWindowsProfile(dir: string, binaryPath?: string, chromePat
if (lockReleased(lock)) { heldWindowsProfiles.delete(dir); return; }
// The Chrome kept last time still runs under the same daemon: every process is not listed again on each turn.
const held = heldWindowsProfiles.get(dir);
- if (held && processAlive(held.browser) && await imageOf(held.daemon) === held.image) return;
+ if (held && processAlive(held.browser) && await imageOf(held.daemon) === held.image && (held.shell === undefined || await imageOf(held.shell) === "cmd.exe")) return;
heldWindowsProfiles.delete(dir);
const listing = await execText("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", WINDOWS_PROFILE_BROWSERS], { OMB_BROWSER_PROFILE: dir }, 15_000);
let rows: unknown;
@@ -273,7 +273,7 @@ async function releaseWindowsProfile(dir: string, binaryPath?: string, chromePat
// A parent started after its child holds a reused pid: the child's own parent is gone.
const parentOf = (name: unknown, parentStarted: unknown, started: unknown) =>
text(name) && text(parentStarted) && text(parentStarted) <= text(started) ? text(name).toLowerCase() : "";
- const browsers = (Array.isArray(rows) ? rows : [rows]).flatMap((row) => {
+ const browsers = (Array.isArray(rows) ? rows : [rows]).flatMap((row): { pid: number; started: string; daemonPid: number; shell?: number; daemon: string }[] => {
const {
ProcessId: pid, ParentProcessId: ppid, CommandLine: line, Name: name, Started: started, ParentName: parent, ParentStarted: parentStarted,
GrandparentId: gppid, GrandparentName: grandparent, GrandparentStarted: grandparentStarted,
@@ -283,15 +283,16 @@ async function releaseWindowsProfile(dir: string, binaryPath?: string, chromePat
const args = windowsArgs(line).map((arg) => arg.toLowerCase());
if (!args.includes(flag) || args.some((arg) => arg.startsWith("--type="))) return [];
const daemon = parentOf(parent, parentStarted, started);
- // Rust's Command runs a .cmd launcher through cmd.exe, so that Chrome's daemon is the shell's parent.
+ // Rust's Command runs a .cmd launcher through cmd.exe, so that Chrome's daemon is the shell's parent. agent-browser 0.36
+ // stops only its child on Windows: a Chrome whose shell it stopped is orphaned under a daemon that still runs.
if (daemon === "cmd.exe") {
- return [{ pid: pid as number, started: started as string, daemonPid: gppid as number, daemon: Number.isInteger(gppid) ? parentOf(grandparent, grandparentStarted, parentStarted) : "" }];
+ return [{ pid: pid as number, started: started as string, daemonPid: gppid as number, shell: ppid as number, daemon: Number.isInteger(gppid) ? parentOf(grandparent, grandparentStarted, parentStarted) : "" }];
}
return [{ pid: pid as number, started: started as string, daemonPid: ppid as number, daemon }];
});
- for (const { pid, started, daemonPid, daemon } of browsers) {
+ for (const { pid, started, daemonPid, shell, daemon } of browsers) {
if (daemon.startsWith("agent-browser") || (binaryPath && daemon === win32.basename(binaryPath).toLowerCase())) {
- heldWindowsProfiles.set(dir, { browser: pid, daemon: daemonPid, image: daemon });
+ heldWindowsProfiles.set(dir, { browser: pid, daemon: daemonPid, image: daemon, shell });
return;
}
// Between the listing and now the pid may have gone to another program: only the same Chrome is stopped.