Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

windows-process-tree.test.mjs

115 lines3.6 KB

1import assert from "node:assert/strict";
2import * as NodeChildProcess from "node:child_process";
3import * as NodeFS from "node:fs";
4import * as NodeOS from "node:os";
5import * as NodePath from "node:path";
6import test from "node:test";
7​
8import {
9 resolveTaskkillExecutable,
10 terminateWindowsProcessTree,
11} from "./windows-process-tree.mjs";
12​
13test("terminateWindowsProcessTree targets only the owned PID tree", () => {
14 const calls = [];
15 const result = terminateWindowsProcessTree(4321, {
16 force: true,
17 env: { SystemRoot: "C:\\Windows" },
18 spawnSync(command, args, options) {
19 calls.push({ command, args, options });
20 return { status: 0 };
21 },
22 });
23​
24 assert.equal(
25 resolveTaskkillExecutable({ SystemRoot: "C:\\Windows" }),
26 "C:\\Windows\\System32\\taskkill.exe",
27 );
28 assert.deepEqual(calls, [
29 {
30 command: "C:\\Windows\\System32\\taskkill.exe",
31 args: ["/PID", "4321", "/T", "/F"],
32 options: { stdio: "ignore", windowsHide: true },
33 },
34 ]);
35 assert.deepEqual(result, { attempted: true, ok: true, status: 0 });
36});
37​
38async function waitForFile(filePath, timeoutMs = 5000) {
39 const deadline = Date.now() + timeoutMs;
40 while (Date.now() < deadline) {
41 if (NodeFS.existsSync(filePath)) return;
42 await new Promise((resolve) => setTimeout(resolve, 25));
43 }
44 throw new Error(`Timed out waiting for ${filePath}`);
46​
47async function waitForExit(child, timeoutMs = 5000) {
48 if (child.exitCode !== null) return;
49 await Promise.race([
50 new Promise((resolve) => child.once("exit", resolve)),
51 new Promise((_, reject) =>
52 setTimeout(() => reject(new Error("Timed out waiting for process exit")), timeoutMs),
53 ),
54 ]);
56​
57function isPidAlive(pid) {
58 try {
59 process.kill(pid, 0);
60 return true;
61 } catch {
62 return false;
63 }
65​
66test(
67 "taskkill /T /F removes real Windows descendant trees across 20 restart cycles",
68 { skip: process.platform !== "win32" },
69 async () => {
70 const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3 tree José "));
71​
72 try {
73 for (let iteration = 0; iteration < 20; iteration += 1) {
74 const receipt = NodePath.join(root, `pids-${iteration}.json`);
75 const parentScript = `
76 const { spawn } = require("node:child_process");
77 const fs = require("node:fs");
78 const child = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], { stdio: "ignore" });
79 fs.writeFileSync(${JSON.stringify(receipt)}, JSON.stringify({ parent: process.pid, child: child.pid }));
80 setInterval(() => {}, 1000);
81 `;
82 const parent = NodeChildProcess.spawn(process.execPath, ["-e", parentScript], {
83 stdio: "ignore",
84 windowsHide: true,
85 });
86​
87 try {
88 await waitForFile(receipt);
89 const pids = JSON.parse(NodeFS.readFileSync(receipt, "utf8"));
90 assert.equal(pids.parent, parent.pid);
91 assert.equal(isPidAlive(pids.child), true);
92​
93 const result = terminateWindowsProcessTree(parent.pid, { force: true });
94 assert.equal(result.ok, true, `taskkill failed on iteration ${iteration}`);
95 await waitForExit(parent);
96​
97 const deadline = Date.now() + 3000;
98 while (Date.now() < deadline && isPidAlive(pids.child)) {
99 await new Promise((resolve) => setTimeout(resolve, 25));
100 }
101 assert.equal(isPidAlive(pids.child), false, `descendant survived iteration ${iteration}`);
102 } finally {
103 if (parent.exitCode === null) parent.kill();
104 }
105 }
106 } finally {
107 NodeFS.rmSync(root, {
108 recursive: true,
109 force: true,
110 maxRetries: 5,
111 retryDelay: 50,
112 });
113 }
114 },
115);