Skip to content

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

Leave the gateway's own children running too, and check again right before each signal

Bugfixer 274aca90e41f0
diff --git a/server/kill-tree.test.ts b/server/kill-tree.test.ts
index ccf3fef241..43b451b4dc 100644
--- a/server/kill-tree.test.ts
+++ b/server/kill-tree.test.ts
@@ -195,6 +195,35 @@ describe("killCliTree", () => {
}
}, 20_000);
+ it.runIf(process.platform === "linux")("leaves running a child the gateway started before its exec, even when recorded first", async () => {
+ // Recorded while the gateway still looked like its launcher, the child then sits in the gateway's session.
+ const parent = spawnCli(process.execPath, ["-e", `
+ const c = require('node:child_process').spawn('/bin/sh', ['-c', '"$0" -e "$2" & echo $!; read go; exec env HERMES_GATEWAY_DETACHED=1 "$0" -e "$1" gateway run', process.execPath, ${JSON.stringify(GATEWAY)}, ${JSON.stringify(IDLE)}], {
+ detached: true, stdio: ['pipe', 'pipe', 'ignore'],
+ });
+ process.stdout.write(c.pid + ' ');
+ process.stdin.once('data', () => c.stdin.write('go\\n'));
+ c.stdout.pipe(process.stdout);
+ ${IDLE};
+ `], { stdio: ["pipe", "pipe", "pipe"] });
+ let output = "";
+ parent.stdout!.on("data", (chunk) => { output += String(chunk); });
+ const pids = () => output.trim().split(/\s+/).filter(Boolean).map(Number);
+ try {
+ await vi.waitFor(() => expect(pids()).toHaveLength(2), 5_000);
+ captureCliTree(parent);
+ parent.stdin!.write("go\n");
+ await vi.waitFor(() => expect(pids()).toHaveLength(4), 5_000);
+ const [gateway, early, reported, late] = pids();
+ expect(reported).toBe(gateway);
+ await expect(killCliTree(parent, 80)).resolves.toBe(true);
+ expect([gateway, early, late].map(alive)).toEqual([true, true, true]);
+ } finally {
+ await killCliTree(parent, 0);
+ if (pids()[0]) try { process.kill(-pids()[0], "SIGKILL"); } catch {}
+ }
+ }, 20_000);
+
it.skipIf(process.platform === "win32")("escalates ignored TERM and shares concurrent stop attempts", async () => {
const child = spawnCli(process.execPath, ["-e", STUBBORN], { stdio: ["pipe", "pipe", "pipe"] });
try {
diff --git a/server/procs.ts b/server/procs.ts
index 496819b997..f1f0561953 100644
--- a/server/procs.ts
+++ b/server/procs.ts
@@ -144,13 +144,26 @@ function outlivesLauncher({ pid, session }: LinuxProcess): boolean {
}
}
+/** Such a daemon, or a process in its session: its own children, even one recorded before the daemon's exec. */
+function leftRunning(current: LinuxProcess): boolean {
+ if (process.platform !== "linux") return false;
+ const { session } = current;
+ if (session === undefined || session === current.pid) return outlivesLauncher(current);
+ try {
+ const leader = linuxProcess(session);
+ return leader !== null && outlivesLauncher(leader);
+ } catch {
+ return false;
+ }
+}
+
/** A child recorded between its fork and its exec still showed its launcher's command line and environment, so a
- * gateway can already be in the tree: forget it before walking the tree. */
+ * gateway and its children can already be in the tree: forget them before walking the tree. */
function forgetOutliving(tree: Map<number, string>): void {
for (const [pid, start] of tree) {
let current: LinuxProcess | null;
try { current = linuxProcess(pid); } catch { continue; }
- if (current?.start === start && outlivesLauncher(current)) tree.delete(pid);
+ if (current?.start === start && leftRunning(current)) tree.delete(pid);
}
}
@@ -187,7 +200,7 @@ function addLinuxDescendants(tree: Map<number, string>): boolean {
let current: LinuxProcess | null;
try { current = linuxProcess(pid); } catch { return false; }
if (!current || current.parent !== parent) return false;
- if (tree.get(pid) === current.start || outlivesLauncher(current)) continue;
+ if (tree.get(pid) === current.start || leftRunning(current)) continue;
tree.set(pid, current.start);
queue.push([pid, current.start]);
}
@@ -216,7 +229,7 @@ function addDescendantsFromEveryProcess(tree: Map<number, string>): void {
discovered = false;
for (const current of snapshot.values()) {
if (current.pid === process.pid || !parents.has(current.parent) || tree.get(current.pid) === current.start) continue;
- if (outlivesLauncher(current)) continue;
+ if (leftRunning(current)) continue;
tree.set(current.pid, current.start);
parents.add(current.pid);
discovered = true;
@@ -231,7 +244,7 @@ function linuxDescendantsStopped(child: ChildProcess): boolean {
for (const [pid, start] of tree) {
try {
const current = read(pid);
- if (current?.start === start && current.state !== "Z" && current.state !== "X" && !outlivesLauncher(current)) return false;
+ if (current?.start === start && current.state !== "Z" && current.state !== "X" && !leftRunning(current)) return false;
} catch { return false; }
}
return true;
@@ -272,7 +285,7 @@ function signalLinuxDescendants(child: ChildProcess, value: NodeJS.Signals): voi
for (const [pid, start] of tree) {
try {
const current = read(pid);
- if (current?.start === start && current.state !== "Z" && current.state !== "X") process.kill(pid, value);
+ if (current?.start === start && current.state !== "Z" && current.state !== "X" && !leftRunning(current)) process.kill(pid, value);
} catch {}
}
}