Fix shell: true side effects on Windows (orphaned processes, swallowed ENOENT)

Add tree-kill via taskkill /T /F in processRunner and codexAppServerManager so timeout enforcement and session stop properly terminate the entire process tree instead of just the cmd.exe wrapper. Detect Windows command not found (exit 9009) in normalizeExitError. Pre-validate editor commands with isCommandAvailable in open.ts before spawning. Fix ProviderHealth test import and dev-runner test path assertion for Windows.

Made-with: Cursor
This commit is contained in:
julius authored and Julius Marminge committed 2026-03-03 21:03:16 -08:00
1 parent 3d0233e3b0
commit 2ae63f4aa2
5 files changed
+82 -28

No files matched your search

+19 -2
View File
@@ -1,4 +1,4 @@
import { type ChildProcessWithoutNullStreams, spawn } from "node:child_process";
import { type ChildProcessWithoutNullStreams, spawn, spawnSync } from "node:child_process";
import { randomUUID } from "node:crypto";
import { EventEmitter } from "node:events";
import readline from "node:readline";
@@ -104,6 +104,23 @@ const RECOVERABLE_THREAD_RESUME_ERROR_SNIPPETS = [
"does not exist",
];
/**
* On Windows with `shell: true`, `child.kill()` only terminates the `cmd.exe`
* wrapper, leaving the actual command running. Use `taskkill /T` to kill the
* entire process tree instead.
*/
function killChildTree(child: ChildProcessWithoutNullStreams): void {
if (process.platform === "win32" && child.pid !== undefined) {
try {
spawnSync("taskkill", ["/pid", String(child.pid), "/T", "/F"], { stdio: "ignore" });
return;
} catch {
// fallback to direct kill
}
}
child.kill();
}
export function normalizeCodexModelSlug(
model: string | undefined | null,
preferredId?: string,
@@ -476,7 +493,7 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
context.output.close();
if (!context.child.killed) {
context.child.kill();
killChildTree(context.child);
}
this.updateSession(context, {
+28 -20
View File
@@ -224,29 +224,37 @@ export const resolveEditorLaunch = Effect.fnUntraced(function* (
});
export const launchDetached = (launch: EditorLaunch) =>
Effect.callback<void, OpenError>((resume) => {
let child;
try {
child = spawn(launch.command, [...launch.args], {
detached: true,
stdio: "ignore",
shell: process.platform === "win32",
});
} catch (error) {
return resume(
Effect.fail(new OpenError({ message: "failed to spawn detached process", cause: error })),
);
Effect.gen(function* () {
if (!isCommandAvailable(launch.command)) {
return yield* new OpenError({ message: `Editor command not found: ${launch.command}` });
}
const handleSpawn = () => {
child.unref();
resume(Effect.void);
};
yield* Effect.callback<void, OpenError>((resume) => {
let child;
try {
child = spawn(launch.command, [...launch.args], {
detached: true,
stdio: "ignore",
shell: process.platform === "win32",
});
} catch (error) {
return resume(
Effect.fail(
new OpenError({ message: "failed to spawn detached process", cause: error }),
),
);
}
child.once("spawn", handleSpawn);
child.once("error", (cause) =>
resume(Effect.fail(new OpenError({ message: "failed to spawn detached process", cause }))),
);
const handleSpawn = () => {
child.unref();
resume(Effect.void);
};
child.once("spawn", handleSpawn);
child.once("error", (cause) =>
resume(Effect.fail(new OpenError({ message: "failed to spawn detached process", cause }))),
);
});
});
const make = Effect.gen(function* () {
+31 -4
View File
@@ -1,4 +1,4 @@
import { spawn } from "node:child_process";
import { type ChildProcess as ChildProcessHandle, spawn, spawnSync } from "node:child_process";
export interface ProcessRunOptions {
cwd?: string | undefined;
@@ -37,11 +37,21 @@ function normalizeSpawnError(command: string, args: readonly string[], error: un
return new Error(`Failed to run ${commandLabel(command, args)}: ${error.message}`);
}
function isWindowsCommandNotFound(code: number | null, stderr: string): boolean {
if (process.platform !== "win32") return false;
if (code === 9009) return true;
return /is not recognized as an internal or external command/i.test(stderr);
}
function normalizeExitError(
command: string,
args: readonly string[],
result: ProcessRunResult,
): Error {
if (isWindowsCommandNotFound(result.code, result.stderr)) {
return new Error(`Command not found: ${command}`);
}
const reason = result.timedOut
? "timed out"
: `failed (code=${result.code ?? "null"}, signal=${result.signal ?? "null"})`;
@@ -70,6 +80,23 @@ function normalizeBufferError(
const DEFAULT_MAX_BUFFER_BYTES = 8 * 1024 * 1024;
/**
* On Windows with `shell: true`, `child.kill()` only terminates the `cmd.exe`
* wrapper, leaving the actual command running. Use `taskkill /T` to kill the
* entire process tree instead.
*/
function killChild(child: ChildProcessHandle, signal: NodeJS.Signals = "SIGTERM"): void {
if (process.platform === "win32" && child.pid !== undefined) {
try {
spawnSync("taskkill", ["/pid", String(child.pid), "/T", "/F"], { stdio: "ignore" });
return;
} catch {
// fallback to direct kill
}
}
child.kill(signal);
}
function appendChunkWithinLimit(
target: string,
currentBytes: number,
@@ -127,9 +154,9 @@ export async function runProcess(
const timeoutTimer = setTimeout(() => {
timedOut = true;
child.kill("SIGTERM");
killChild(child, "SIGTERM");
forceKillTimer = setTimeout(() => {
child.kill("SIGKILL");
killChild(child, "SIGKILL");
}, 1_000);
}, timeoutMs);
@@ -144,7 +171,7 @@ export async function runProcess(
};
const fail = (error: Error): void => {
child.kill("SIGTERM");
killChild(child, "SIGTERM");
finalize(() => {
reject(error);
});
@@ -1,4 +1,5 @@
import { assert, it } from "@effect/vitest";
import assert from "node:assert/strict";
import { it } from "@effect/vitest";
import { Effect, Layer, Sink, Stream } from "effect";
import * as PlatformError from "effect/PlatformError";
import { ChildProcessSpawner } from "effect/unstable/process";
+2 -1
View File
@@ -1,4 +1,5 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { resolve } from "node:path";
import { assert, describe, it } from "@effect/vitest";
import { Effect } from "effect";
@@ -86,7 +87,7 @@ it.layer(NodeServices.layer)("dev-runner", (it) => {
devUrl: new URL("http://localhost:7331"),
});
assert.equal(env.T3CODE_STATE_DIR, "/tmp/override-state");
assert.equal(env.T3CODE_STATE_DIR, resolve("/tmp/override-state"));
assert.equal(env.T3CODE_PORT, "4222");
assert.equal(env.VITE_WS_URL, "ws://localhost:4222");
assert.equal(env.T3CODE_NO_BROWSER, "1");