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

address review (codex): never taskkill an exited win32 child, and keep signal 0 a probe

Adam Eivyffa42fc89b5e
diff --git a/server/lib/detachedSpawn.js b/server/lib/detachedSpawn.js
index 128c5ae269..0dd9f511f4 100644
--- a/server/lib/detachedSpawn.js
+++ b/server/lib/detachedSpawn.js
@@ -270,6 +270,12 @@ export async function spawnDetached(bin, args = [], {
// failure reason — so re-stamp the signal we asked for onto the terminal
// events, matching both a native kill and the POSIX handle's decoded close.
// A concurrent clean exit (code 0) is left alone: the job really did finish.
+ // The stamp records the signal we ASKED for without waiting to confirm the
+ // tree died from it — exactly what Node's own `kill()` does (libuv stamps
+ // exit_signal at request time), and the guard above already refuses to fire
+ // at an exited child, so the only ambiguity left is a nonzero exit racing
+ // our kill by microseconds. Both readings mean "cancelled", so no async
+ // taskkill-completion plumbing is warranted here.
let killSignal = null;
const nativeEmit = child.emit.bind(child);
child.emit = (event, ...rest) => {
@@ -281,6 +287,15 @@ export async function spawnDetached(bin, args = [], {
return nativeEmit(event, ...rest);
};
child.kill = (signal = 'SIGTERM') => {
+ // `taskkill /T /F` is destructive and Windows recycles PIDs freely, so it
+ // must never fire at a child that already exited — a late escalation
+ // (killWithEscalation's 8s SIGKILL) would tree-kill whatever inherited the
+ // number. Node's own kill is safe there because it holds a process HANDLE,
+ // not a pid; taskkill only gets the pid.
+ if (!child.pid || child.exitCode !== null || child.signalCode !== null) return false;
+ // Signal 0 is an existence PROBE, not a kill — hand it to Node so callers
+ // keep that meaning instead of force-killing the tree.
+ if (signal === 0 || signal === '0') return nativeKill(signal);
killSignal = signal;
child.killed = true;
killProcessTree(treeKillTarget, signal, { processGroup: true });
diff --git a/server/lib/detachedSpawn.test.js b/server/lib/detachedSpawn.test.js
index 2b4a65b9f9..b85d76e016 100644
--- a/server/lib/detachedSpawn.test.js
+++ b/server/lib/detachedSpawn.test.js
@@ -312,6 +312,40 @@ describe('spawnDetached', () => {
}
});
+ it('win32 fallback refuses to tree-kill a child that already exited', async () => {
+ const restorePlatform = pinPlatform('win32');
+ try {
+ const { handle, terminate } = await spawnWin32Fallback();
+ const closed = onClose(handle);
+ await terminate();
+ await closed;
+ // Windows recycles PIDs, so a late escalation must not taskkill whatever
+ // inherited the number.
+ killProcessTree.mockClear();
+ expect(handle.kill('SIGKILL')).toBe(false);
+ expect(killProcessTree).not.toHaveBeenCalled();
+ } finally {
+ restorePlatform();
+ }
+ });
+
+ it('win32 fallback treats signal 0 as an existence probe, not a kill', async () => {
+ const restorePlatform = pinPlatform('win32');
+ try {
+ killProcessTree.mockClear();
+ const { handle, terminate } = await spawnWin32Fallback();
+ const closed = onClose(handle);
+ // Node's own kill(0) answers the probe (and sets `killed`, as it always
+ // has); what matters is that no taskkill went out.
+ expect(handle.kill(0)).toBe(true);
+ expect(killProcessTree).not.toHaveBeenCalled();
+ await terminate();
+ await closed;
+ } finally {
+ restorePlatform();
+ }
+ });
+
it.runIf(IS_POSIX)('leaves the POSIX path on its own pid/group signalling (no tree-kill)', async () => {
killProcessTree.mockClear();
const controlDir = await tmpControlDir();