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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (claude): don't taskkill a non-ChildProcess killable
[._.]/ Adam Eivya3c5df17a58b
diff --git a/server/lib/aiToolkit/runner.js b/server/lib/aiToolkit/runner.jsindex b987222089..8978b1961b 100644--- a/server/lib/aiToolkit/runner.js+++ b/server/lib/aiToolkit/runner.js@@ -2,7 +2,7 @@ import { mkdir, readFile, readdir, rm } from 'fs/promises';import { atomicWrite } from './internal/atomicWrite.js';import { existsSync } from 'fs';import { join, extname, basename, isAbsolute, delimiter } from 'path';-import { spawn } from 'child_process';+import { spawn, ChildProcess } from 'child_process';import { randomUUID } from 'crypto';import { analyzeError, analyzeHttpError, ERROR_CATEGORIES } from './errorDetection.js';@@ -46,8 +46,16 @@ function resolveWindowsExecutable(command, isWin32 = IS_WIN32) {// `.killed` actually engage on Windows. A plain SIGTERM kills the cmd.exe// wrapper but orphans the real child, so the whole process tree is taken// down via `taskkill /T /F` there; SIGTERM is sufficient elsewhere.+//+// The win32 branch is gated on `instanceof ChildProcess`: stopRun/deleteRun+// below call this with whatever was registered via registerExternalRun,+// which can be a node-pty `IPty` TUI session (server/lib/tuiPromptRunner.js,+// via the host's registerActiveRun) — it also exposes `.kill()`/`.pid`, but+// a raw `taskkill` against its pid bypasses node-pty's own Windows teardown+// (releasing its native ConPTY handle), leaking it. Any non-ChildProcess+// killable always uses its own `.kill()` instead, on every platform.function killProcessTree(child) {- if (IS_WIN32 && child.pid) {+ if (IS_WIN32 && child.pid && child instanceof ChildProcess) {child.killed = true;spawn('taskkill', ['/T', '/F', '/PID', String(child.pid)], { stdio: 'ignore', windowsHide: true }).on('error', () => {})diff --git a/server/lib/bufferedSpawn.js b/server/lib/bufferedSpawn.jsindex 078c09ab66..3fb7061d73 100644--- a/server/lib/bufferedSpawn.js+++ b/server/lib/bufferedSpawn.js@@ -1,4 +1,4 @@-import { spawn } from 'child_process';+import { spawn, ChildProcess } from 'child_process';import { existsSync } from 'fs';import { delimiter, isAbsolute, join } from 'path';@@ -99,10 +99,18 @@ export const MAX_OUTPUT_BYTES = 64 * 1024;* object, so without this the flag would stay `false` for the process's* entire lifetime and those guards would never engage on Windows.*+ * The win32 branch is gated on `instanceof ChildProcess` — some callers (the+ * aiToolkit runner's `stopRun`, via `registerExternalRun`) pass this a+ * killable that isn't a `child_process` spawn at all, e.g. a node-pty `IPty`+ * TUI session, which also exposes `.kill()`/`.pid`. A raw `taskkill` against+ * a pty's pid bypasses node-pty's own Windows teardown (releasing its native+ * ConPTY handle), leaking it — so any non-ChildProcess killable always uses+ * its own `.kill()` instead, on every platform.+ ** @param {import('child_process').ChildProcess} child*/export function killProcessTree(child) {- if (IS_WIN32 && child.pid) {+ if (IS_WIN32 && child.pid && child instanceof ChildProcess) {child.killed = true;spawn('taskkill', ['/T', '/F', '/PID', String(child.pid)], { stdio: 'ignore', windowsHide: true }).on('error', () => {})diff --git a/server/lib/bufferedSpawn.test.js b/server/lib/bufferedSpawn.test.jsindex 87935163b7..9b8585bd5f 100644--- a/server/lib/bufferedSpawn.test.js+++ b/server/lib/bufferedSpawn.test.js@@ -1,13 +1,20 @@import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';import { EventEmitter } from 'events';+import { ChildProcess } from 'child_process';import { mkdtemp, rm, writeFile } from 'fs/promises';import { join } from 'path';import { tmpdir } from 'os';// Mock child_process.spawn so we can drive the buffered-spawn machinery without-// launching real processes.+// launching real processes. Pass the real module through (importOriginal) and+// override only `spawn` — killProcessTree's `instanceof ChildProcess` guard+// needs the real `ChildProcess` export; a from-scratch replacement object+// (no `ChildProcess` key) would make that check throw on an actual win32 run.const spawnMock = vi.fn();-vi.mock('child_process', () => ({ spawn: (...a) => spawnMock(...a) }));+vi.mock('child_process', async (importOriginal) => {+ const actual = await importOriginal();+ return { ...actual, spawn: (...a) => spawnMock(...a) };+});// Re-imported after the mock is registered.const {@@ -21,9 +28,16 @@ const {MAX_OUTPUT_BYTES,} = await import('./bufferedSpawn.js');-/** Build a fake child process with stdout/stderr emitters and a kill spy. */+/**+ * Build a fake child process with stdout/stderr emitters and a kill spy.+ * Its prototype is swapped to ChildProcess.prototype so it passes+ * killProcessTree's `instanceof ChildProcess` guard exactly like a real+ * spawn() result would — without this, the "spawns taskkill" test below+ * would silently take the SIGTERM fallback branch on an actual win32 run.+ */function makeFakeChild({ pid = 1234 } = {}) {const child = new EventEmitter();+ Object.setPrototypeOf(child, ChildProcess.prototype);child.pid = pid;child.stdout = new EventEmitter();child.stderr = new EventEmitter();@@ -86,6 +100,18 @@ describe('killProcessTree', () => {// elsewhere (gated on `!child.killed`) actually engage on Windows.expect(child.killed).toBe(true);});++ it('on Windows still uses .kill() (not taskkill) for a non-ChildProcess killable, e.g. a node-pty session', () => {+ if (!IS_WIN32) return; // can't simulate platform branch from outside+ // A killable that exposes .kill()/.pid like a ChildProcess but isn't one+ // (e.g. node-pty's IPty, registered via registerExternalRun for TUI+ // runs) — taskkill against its pid would bypass its own native teardown+ // (releasing a Windows ConPTY handle) and leak it.+ const ptyLike = { pid: 4321, kill: vi.fn() };+ killProcessTree(ptyLike);+ expect(spawnMock).not.toHaveBeenCalled();+ expect(ptyLike.kill).toHaveBeenCalledWith('SIGTERM');+ });});describe('resolveWindowsExecutable', () => {