Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(procs): count a Windows CLI stopped once it exits when taskkill /T fails (#4270)

Aditya Umale2bd074d3a8ce
diff --git a/server/procs-windows-stop.test.ts b/server/procs-windows-stop.test.ts
new file mode 100644
index 0000000000..0ff9956fac
--- /dev/null
+++ b/server/procs-windows-stop.test.ts
@@ -0,0 +1,61 @@
+import { EventEmitter } from "node:events";
+import type { ChildProcess } from "node:child_process";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+vi.mock("node:child_process", async (original) => {
+ const actual = await original<typeof import("node:child_process")>();
+ return { ...actual, execFile: vi.fn() };
+});
+
+const { execFile } = await import("node:child_process");
+const { killCliTree } = await import("./procs.ts");
+
+const platform = process.platform;
+afterEach(() => {
+ Object.defineProperty(process, "platform", { value: platform, configurable: true });
+ vi.mocked(execFile).mockReset();
+});
+
+/** A CLI on Windows: it exits when stopped, but a process left in its tree still holds the pipes it inherited, so "close" never comes. */
+function windowsCli(pid: number): ChildProcess {
+ const child = Object.assign(new EventEmitter(), { pid, exitCode: null as number | null, signalCode: null, kill: () => true });
+ child.kill = () => {
+ queueMicrotask(() => { child.exitCode = 1; child.emit("exit", 1, null); });
+ return true;
+ };
+ return child as unknown as ChildProcess;
+}
+
+describe("stopping a CLI on Windows", () => {
+ it("counts the CLI stopped once it exits, when taskkill fails for a process it cannot stop in the tree", async () => {
+ Object.defineProperty(process, "platform", { value: "win32", configurable: true });
+ // taskkill /T /F exits non-zero when any process in the tree could not be stopped (already gone, or access denied).
+ vi.mocked(execFile).mockImplementation(((file: string, args: string[], _options: unknown, done: (error: Error | null) => void) => {
+ expect(file).toBe("taskkill");
+ expect(args).toEqual(["/PID", "4300", "/T", "/F"]);
+ queueMicrotask(() => done(Object.assign(new Error("ERROR: The process with PID 4321 (child process of PID 4300) could not be terminated."), { code: 128 })));
+ return {} as ChildProcess;
+ }) as unknown as typeof execFile);
+ const child = windowsCli(4300);
+ const started = Date.now();
+ await expect(killCliTree(child, 2_000)).resolves.toBe(true);
+ expect(Date.now() - started).toBeLessThan(1_500);
+ });
+
+ it("counts the CLI stopped when it exits before taskkill reports the failure", async () => {
+ Object.defineProperty(process, "platform", { value: "win32", configurable: true });
+ const child = windowsCli(4300);
+ const raw = child as unknown as EventEmitter & { exitCode: number | null };
+ // taskkill stopped the CLI itself, then failed on a process left in its tree.
+ vi.mocked(execFile).mockImplementation(((file: string, args: string[], _options: unknown, done: (error: Error | null) => void) => {
+ expect([file, ...args]).toEqual(["taskkill", "/PID", "4300", "/T", "/F"]);
+ raw.exitCode = 1;
+ raw.emit("exit", 1, null);
+ setTimeout(() => done(Object.assign(new Error("ERROR: The process with PID 4321 (child process of PID 4300) could not be terminated."), { code: 128 })), 50);
+ return {} as ChildProcess;
+ }) as unknown as typeof execFile);
+ const started = Date.now();
+ await expect(killCliTree(child, 2_000)).resolves.toBe(true);
+ expect(Date.now() - started).toBeLessThan(1_500);
+ });
+});
diff --git a/server/procs.ts b/server/procs.ts
index bee4ea3818..56df248712 100644
--- a/server/procs.ts
+++ b/server/procs.ts
@@ -378,6 +378,10 @@ async function stopCliTree(child: ChildProcess, pid: number, timeoutMs: number):
child.kill();
} catch {
}
+ // taskkill /T also fails when one process in the tree could not be stopped; that one may still hold the pipes the CLI
+ // inherited, so "close" never comes. The CLI itself exiting is what counts.
+ if (exited()) done(true);
+ else child.once("exit", closed);
});
});
}