refactor(engine): manage child process lifecycles (#2160)

* refactor(engine): manage child process lifecycles

* fix(engine): preserve child reaping after runtime errors

* fix(engine): untrack child processes on exit
This commit is contained in:
James Russo
2026-07-17 01:17:53 -04:00
committed by GitHub
parent 8e162921bc
commit 57d3bf4960
17 changed files with 697 additions and 537 deletions
@@ -1,4 +1,4 @@
import { describe, it, expect, beforeEach } from "vitest";
import { describe, it, expect, beforeEach, vi } from "vitest";
import { spawn } from "node:child_process";
import { trackChildProcess, killTrackedProcesses } from "./processTracker.js";
@@ -18,14 +18,45 @@ describe("trackChildProcess", () => {
killTrackedProcesses();
});
it("removes the process on spawn error", async () => {
const proc = spawn("/nonexistent-binary-that-does-not-exist", { stdio: "ignore" });
it("removes an exited process before its stdio closes", async () => {
const proc = spawn("sleep", ["60"], { stdio: "ignore" });
const closePromise = new Promise<void>((resolve) => proc.on("close", resolve));
const kill = vi.spyOn(proc, "kill");
trackChildProcess(proc);
await new Promise<void>((resolve) => proc.on("error", () => resolve()));
try {
proc.emit("exit", 0, null);
killTrackedProcesses();
expect(kill).not.toHaveBeenCalled();
} finally {
kill.mockRestore();
proc.kill("SIGKILL");
await closePromise;
}
});
it("removes the process on spawn error", async () => {
const proc = spawn("/nonexistent-binary-that-does-not-exist", { stdio: "ignore" });
proc.on("error", () => undefined);
trackChildProcess(proc);
await new Promise<void>((resolve) => proc.on("close", () => resolve()));
killTrackedProcesses();
});
it("keeps a process tracked after a post-spawn error", () => {
const proc = spawn("sleep", ["60"], { stdio: "ignore" });
const kill = vi.spyOn(proc, "kill");
proc.on("error", () => undefined);
trackChildProcess(proc);
proc.emit("error", new Error("kill EPERM"));
killTrackedProcesses();
expect(kill).toHaveBeenCalledWith("SIGTERM");
});
});
describe("killTrackedProcesses", () => {