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 (claude): re-stamp the kill signal on the win32 handle's terminal events

Adam Eivy6a04a18a0ba1
diff --git a/server/lib/detachedSpawn.js b/server/lib/detachedSpawn.js
index 43a347276d..128c5ae269 100644
--- a/server/lib/detachedSpawn.js
+++ b/server/lib/detachedSpawn.js
@@ -244,7 +244,8 @@ export async function spawnDetached(bin, args = [], {
// back to a normal child process: a real ChildProcess already satisfies the
// handle contract (pid / stdout / stderr / on('close',code,signal) / kill /
// exitCode / signalCode), so callers are unaffected. Surviving a pm2 restart
- // is a POSIX-only guarantee; Windows keeps its prior spawn semantics.
+ // is a POSIX-only guarantee; Windows keeps its prior spawn semantics apart
+ // from `kill`, which is replaced with a tree-kill below.
if (process.platform === 'win32') {
const child = spawn(bin, args, { env: withSpawnCwdEnv(env ?? process.env, cwd), cwd, stdio: ['ignore', 'pipe', 'pipe'] });
// A bare `child.kill()` terminates ONLY the runner. Windows has no process
@@ -261,7 +262,26 @@ export async function spawnDetached(bin, args = [], {
// gated on it) but exposes Node's own kill, so killProcessTree's POSIX
// fall-through can never re-enter the override below.
const treeKillTarget = Object.create(child, { kill: { value: nativeKill } });
+ // `taskkill` terminates the tree OUT OF BAND, so libuv never records an
+ // exit_signal and the child reports `close(1, null)` where Node's own
+ // `kill()` reported `close(null, 'SIGKILL')`. Callers classify on exactly
+ // that signal — videoGen's watchdog-success test keeps a finished .mp4 only
+ // when `signal === 'SIGKILL'`, and `describeSignalDeath` reads it for the
+ // 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.
+ let killSignal = null;
+ const nativeEmit = child.emit.bind(child);
+ child.emit = (event, ...rest) => {
+ if (killSignal && (event === 'close' || event === 'exit') && rest[1] == null && rest[0] !== 0) {
+ child.exitCode = null;
+ child.signalCode = killSignal;
+ return nativeEmit(event, null, killSignal);
+ }
+ return nativeEmit(event, ...rest);
+ };
child.kill = (signal = 'SIGTERM') => {
+ killSignal = signal;
child.killed = true;
killProcessTree(treeKillTarget, signal, { processGroup: true });
return true;
diff --git a/server/lib/detachedSpawn.test.js b/server/lib/detachedSpawn.test.js
index ea368f1876..2b4a65b9f9 100644
--- a/server/lib/detachedSpawn.test.js
+++ b/server/lib/detachedSpawn.test.js
@@ -228,12 +228,25 @@ describe('spawnDetached', () => {
}
});
+ // A child that runs until a marker file appears, then exits with a code and
+ // NO signal — the shape `taskkill /T /F` produces on Windows, where the kill
+ // happens out of band so libuv records no exit_signal.
+ const spawnWin32Fallback = async () => {
+ const controlDir = await tmpControlDir();
+ const marker = join(controlDir, 'go');
+ const handle = await spawnDetached(
+ 'sh',
+ ['-c', 'while [ ! -f "$1" ]; do sleep 0.05; done; exit 1', 'sh', marker],
+ { controlDir }
+ );
+ return { handle, terminate: () => writeFile(marker, '1') };
+ };
+
it('win32 fallback kill() tree-kills so the runner\'s children die with it', async () => {
const restorePlatform = pinPlatform('win32');
try {
killProcessTree.mockClear();
- const controlDir = await tmpControlDir();
- const handle = await spawnDetached('sh', ['-c', 'sleep 30'], { controlDir });
+ const { handle, terminate } = await spawnWin32Fallback();
const closed = onClose(handle);
expect(handle.kill('SIGKILL')).toBe(true);
expect(handle.killed).toBe(true);
@@ -248,14 +261,57 @@ describe('spawnDetached', () => {
expect(target).toBeInstanceOf(ChildProcess);
expect(target.pid).toBe(handle.pid);
expect(target.kill).not.toBe(handle.kill);
- // killProcessTree is stubbed, so nothing actually died — reap the sleeper.
- target.kill('SIGKILL');
+ await terminate();
await closed;
} finally {
restorePlatform();
}
});
+ it('win32 fallback reports the requested signal on close (taskkill kills out of band)', async () => {
+ const restorePlatform = pinPlatform('win32');
+ try {
+ killProcessTree.mockClear();
+ const { handle, terminate } = await spawnWin32Fallback();
+ const closed = onClose(handle);
+ handle.kill('SIGKILL');
+ // The stubbed tree-kill didn't terminate anything; let the child exit the
+ // way a taskkill'd one does — a plain non-zero code, no signal.
+ await terminate();
+ const { code, signal } = await closed;
+ // Without the re-stamp this is (1, null) and videoGen discards a finished
+ // render as "Exit code 1" instead of honoring the watchdog kill.
+ expect(code).toBeNull();
+ expect(signal).toBe('SIGKILL');
+ expect(handle.exitCode).toBeNull();
+ expect(handle.signalCode).toBe('SIGKILL');
+ } finally {
+ restorePlatform();
+ }
+ });
+
+ it('win32 fallback leaves a clean exit alone when a cancel races completion', async () => {
+ const restorePlatform = pinPlatform('win32');
+ try {
+ killProcessTree.mockClear();
+ const controlDir = await tmpControlDir();
+ const marker = join(controlDir, 'go');
+ const handle = await spawnDetached(
+ 'sh',
+ ['-c', 'while [ ! -f "$1" ]; do sleep 0.05; done; exit 0', 'sh', marker],
+ { controlDir }
+ );
+ const closed = onClose(handle);
+ handle.kill('SIGTERM');
+ await writeFile(marker, '1');
+ const { code, signal } = await closed;
+ expect(code).toBe(0);
+ expect(signal).toBeNull();
+ } 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();