Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: escalate an expired probe's SIGKILL to its whole process group (#10604)

[._.]/ Adam Eivycf8dff37b6d6
diff --git a/server/lib/commandExists.js b/server/lib/commandExists.js
index f69ef7a0ca..636323ba4c 100644
--- a/server/lib/commandExists.js
+++ b/server/lib/commandExists.js
@@ -38,7 +38,7 @@ const PROBE_KILL_GRACE_MS = 2_000;
* at once and terminates only the child it spawned: on Windows
* `killProcessTree` takes the whole `cmd.exe` shim tree with `taskkill /T`;
* on POSIX the child runs in its own process group (`detached`), which gets
- * SIGTERM and, if the child is still alive after a short grace, SIGKILL.
+ * SIGTERM and, after a short grace, SIGKILL for anything in it still alive.
* Output past `maxBuffer` fails the probe the same way. Every event after the
* result is fixed — a late `error`, `close` or chunk — is ignored, and a
* failure to signal or to clean up never replaces the result.
@@ -125,8 +125,17 @@ function runProbe({ prepareCliSpawn, killProcessTree, IS_WIN32 }, cmd, args, pro
const terminate = () => {
signal('SIGTERM');
if (IS_WIN32) return; // taskkill /T /F already force-killed the tree
+ if (!child.pid) return;
+ // Escalate on the GROUP, not the direct child's exit status: a CLI that
+ // exits on SIGTERM can leave a descendant that ignored it. The group id
+ // cannot be reused while any member lives, so ESRCH just means the whole
+ // group is already gone — never a different process.
const escalation = setTimeout(() => {
- if (child.exitCode === null && child.signalCode === null) signal('SIGKILL');
+ try {
+ process.kill(-child.pid, 'SIGKILL');
+ } catch {
+ // ESRCH — every process in the group has exited.
+ }
}, PROBE_KILL_GRACE_MS);
escalation.unref?.();
};
diff --git a/server/lib/commandExists.test.js b/server/lib/commandExists.test.js
index 1649cef08d..2149530c13 100644
--- a/server/lib/commandExists.test.js
+++ b/server/lib/commandExists.test.js
@@ -209,6 +209,15 @@ describe('commandOutput lifecycle (#10604)', () => {
await expect(commandOutput('tool')).resolves.toBe('v1')
})
+ // Real processes, POSIX only: the deadline must settle the probe on time, and
+ // escalation must reach everything in the probe's own process group.
+ const waitUntilGone = async (pid) => {
+ const alive = () => { try { process.kill(pid, 0); return true } catch { return false } }
+ const until = Date.now() + 5_000
+ while (alive() && Date.now() < until) await new Promise((r) => setTimeout(r, 50))
+ return !alive()
+ }
+
it.skipIf(process.platform === 'win32')('a real child that ignores SIGTERM cannot hold the probe past its deadline', async () => {
spawnMock.impl = null
const script = "process.on('SIGTERM', () => {}); setInterval(() => {}, 1000); process.stdout.write('alive')"
@@ -218,11 +227,29 @@ describe('commandOutput lifecycle (#10604)', () => {
expect(Date.now() - started).toBeLessThan(2_000)
expect(existsSync(lastSpawn().opts.env.TMPDIR)).toBe(false)
- // The owned child is escalated to SIGKILL after the grace period.
- const pid = lastSpawn().child?.pid
- const alive = () => { try { process.kill(pid, 0); return true } catch { return false } }
- const killDeadline = Date.now() + 5_000
- while (alive() && Date.now() < killDeadline) await new Promise((r) => setTimeout(r, 50))
- expect(alive()).toBe(false)
+ expect(await waitUntilGone(lastSpawn().child.pid)).toBe(true)
+ }, 10_000)
+
+ it.skipIf(process.platform === 'win32')('escalation reaches a descendant that ignores SIGTERM after its parent exits', async () => {
+ spawnMock.impl = null
+ const { mkdtempSync, readFileSync, rmSync } = await vi.importActual('node:fs')
+ const { tmpdir } = await import('node:os')
+ const { join } = await import('node:path')
+ const dir = mkdtempSync(join(tmpdir(), 'probe-test-'))
+ const pidFile = join(dir, 'grandchild.pid')
+ const grandchild = "process.on('SIGTERM', () => {}); setInterval(() => {}, 1000)"
+ // The parent keeps the default SIGTERM behavior (exits); its child ignores it.
+ const parent = `const c = require('child_process').spawn(process.execPath, ['-e', ${JSON.stringify(grandchild)}], { stdio: 'ignore' });
+ require('fs').writeFileSync(${JSON.stringify(pidFile)}, String(c.pid)); setInterval(() => {}, 1000)`
+
+ try {
+ await expect(commandOutput(process.execPath, ['-e', parent], { timeoutMs: 500 })).resolves.toBeNull()
+
+ const grandchildPid = Number(readFileSync(pidFile, 'utf8'))
+ expect(grandchildPid).toBeGreaterThan(0)
+ expect(await waitUntilGone(grandchildPid)).toBe(true)
+ } finally {
+ rmSync(dir, { recursive: true, force: true })
+ }
}, 10_000)
})