fix: resolve Windows ENOENT when spawning codex app-server (#55)
* fix: add shell and windowsHide options for Windows spawn in app-server
On Windows, spawn("codex", ["app-server"]) fails with ENOENT because
Node.js cannot resolve .cmd shims without shell: true. This adds
platform-gated shell and windowsHide options to the app-server spawn
call, and uses terminateProcessTree for proper process tree cleanup
since shell: true wraps the child in cmd.exe.
Without terminateProcessTree, plain SIGTERM only kills cmd.exe and
leaves the actual codex node process orphaned — verified with 274+
zombie node.exe processes accumulating on Windows.
Fixes #32
Fixes #46
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: guard terminateProcessTree against PID reuse after process exit
ChildProcess.killed only reflects whether .kill() was called by this
process — it stays false when the child exits on its own. On Windows,
where PIDs are recycled quickly, the 50 ms timer could fire after
cmd.exe has exited and its PID has been reassigned, causing taskkill
to terminate an unrelated process.
Add an exitCode === null check so the tree-kill path is skipped once
the child has already exited.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
19642ad95f
commit
1a79ae57ec
@ -14,6 +14,7 @@ import { spawn } from "node:child_process";
|
|||||||
import readline from "node:readline";
|
import readline from "node:readline";
|
||||||
import { parseBrokerEndpoint } from "./broker-endpoint.mjs";
|
import { parseBrokerEndpoint } from "./broker-endpoint.mjs";
|
||||||
import { ensureBrokerSession } from "./broker-lifecycle.mjs";
|
import { ensureBrokerSession } from "./broker-lifecycle.mjs";
|
||||||
|
import { terminateProcessTree } from "./process.mjs";
|
||||||
|
|
||||||
const PLUGIN_MANIFEST_URL = new URL("../../.claude-plugin/plugin.json", import.meta.url);
|
const PLUGIN_MANIFEST_URL = new URL("../../.claude-plugin/plugin.json", import.meta.url);
|
||||||
const PLUGIN_MANIFEST = JSON.parse(fs.readFileSync(PLUGIN_MANIFEST_URL, "utf8"));
|
const PLUGIN_MANIFEST = JSON.parse(fs.readFileSync(PLUGIN_MANIFEST_URL, "utf8"));
|
||||||
@ -188,7 +189,9 @@ class SpawnedCodexAppServerClient extends AppServerClientBase {
|
|||||||
this.proc = spawn("codex", ["app-server"], {
|
this.proc = spawn("codex", ["app-server"], {
|
||||||
cwd: this.cwd,
|
cwd: this.cwd,
|
||||||
env: this.options.env,
|
env: this.options.env,
|
||||||
stdio: ["pipe", "pipe", "pipe"]
|
stdio: ["pipe", "pipe", "pipe"],
|
||||||
|
shell: process.platform === "win32",
|
||||||
|
windowsHide: true
|
||||||
});
|
});
|
||||||
|
|
||||||
this.proc.stdout.setEncoding("utf8");
|
this.proc.stdout.setEncoding("utf8");
|
||||||
@ -237,8 +240,20 @@ class SpawnedCodexAppServerClient extends AppServerClientBase {
|
|||||||
if (this.proc && !this.proc.killed) {
|
if (this.proc && !this.proc.killed) {
|
||||||
this.proc.stdin.end();
|
this.proc.stdin.end();
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (this.proc && !this.proc.killed) {
|
if (this.proc && !this.proc.killed && this.proc.exitCode === null) {
|
||||||
this.proc.kill("SIGTERM");
|
// On Windows with shell: true, the direct child is cmd.exe.
|
||||||
|
// Use terminateProcessTree to kill the entire tree including
|
||||||
|
// the grandchild node process.
|
||||||
|
if (process.platform === "win32") {
|
||||||
|
try {
|
||||||
|
terminateProcessTree(this.proc.pid);
|
||||||
|
} catch {
|
||||||
|
// Best-effort cleanup inside an unref'd timer — swallow errors
|
||||||
|
// to avoid crashing the host process during shutdown.
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.proc.kill("SIGTERM");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, 50).unref?.();
|
}, 50).unref?.();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,7 +8,8 @@ export function runCommand(command, args = [], options = {}) {
|
|||||||
encoding: "utf8",
|
encoding: "utf8",
|
||||||
input: options.input,
|
input: options.input,
|
||||||
stdio: options.stdio ?? "pipe",
|
stdio: options.stdio ?? "pipe",
|
||||||
shell: process.platform === "win32"
|
shell: process.platform === "win32",
|
||||||
|
windowsHide: true
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user