Skip to content

Write HTML. Render video. Built for agents.

test(cli): process tests stop owning old processes that carry a reused parent pid on Windows (#6111)

Miguel Ángel784e1d01d94c
diff --git a/packages/cli/src/utils/processTestUtils.test.ts b/packages/cli/src/utils/processTestUtils.test.ts
new file mode 100644
index 0000000000..5cdbc5012c
--- /dev/null
+++ b/packages/cli/src/utils/processTestUtils.test.ts
@@ -0,0 +1,44 @@
+import { expect, it } from "vitest";
+import { ownedDescendants, parseProcessListing } from "./processTestUtils.js";
+
+const entry = (parent: number, started: bigint) => ({ parent, name: "node.exe", started });
+
+it("does not own a process that started before the parent whose pid it carries", () => {
+ const table = new Map([
+ [100, entry(1, 50n)],
+ [200, entry(100, 60n)],
+ [300, entry(100, 10n)],
+ [400, entry(300, 70n)],
+ ]);
+ expect(ownedDescendants(table, 100)).toEqual([200]);
+});
+
+it("owns the whole tree where every child started after its parent", () => {
+ const table = new Map([
+ [100, entry(1, 50n)],
+ [200, entry(100, 60n)],
+ [300, entry(200, 70n)],
+ ]);
+ expect(ownedDescendants(table, 100)).toEqual([200, 300]);
+});
+
+it("owns children by parent pid alone where no start is listed", () => {
+ const table = new Map([
+ [100, { parent: 1, name: "node" }],
+ [200, { parent: 100, name: "chrome" }],
+ ]);
+ expect(ownedDescendants(table, 100)).toEqual([200]);
+});
+
+it("reads each pid, parent, start and name from a Windows listing", () => {
+ const table = parseProcessListing(
+ "100 1 639000000000000001 node.exe\n200 100 639000000000000002 chrome-headless-shell.exe\n",
+ true,
+ );
+ expect(table.get(200)).toEqual({
+ parent: 100,
+ name: "chrome-headless-shell.exe",
+ started: 639000000000000002n,
+ });
+ expect(ownedDescendants(table, 100)).toEqual([200]);
+});
diff --git a/packages/cli/src/utils/processTestUtils.ts b/packages/cli/src/utils/processTestUtils.ts
index 8cb1a275b9..d3156acc47 100644
--- a/packages/cli/src/utils/processTestUtils.ts
+++ b/packages/cli/src/utils/processTestUtils.ts
@@ -27,8 +27,14 @@ export async function waitForTestCondition(check: () => boolean, timeoutMs: numb
}
}
-function processTable(): Map<number, { parent: number; name: string }> {
- const table = new Map<number, { parent: number; name: string }>();
+interface TableEntry {
+ parent: number;
+ name: string;
+ started?: bigint;
+}
+
+function processTable(): Map<number, TableEntry> {
+ const table = new Map<number, TableEntry>();
if (process.platform === "linux") {
for (const name of readdirSync("/proc")) {
if (!/^\d+$/.test(name)) continue;
@@ -49,26 +55,45 @@ function processTable(): Map<number, { parent: number; name: string }> {
[
"-NoProfile",
"-Command",
- 'Get-CimInstance Win32_Process | ForEach-Object { "$($_.ProcessId) $($_.ParentProcessId) $($_.Name)" }',
+ 'Get-CimInstance Win32_Process | ForEach-Object { "$($_.ProcessId) $($_.ParentProcessId) $($_.CreationDate.Ticks) $($_.Name)" }',
],
{ encoding: "utf8" },
)
: execFileSync("ps", ["-A", "-o", "pid=,ppid=,comm="], { encoding: "utf8" });
+ return parseProcessListing(listing, process.platform === "win32");
+}
+
+export function parseProcessListing(listing: string, withStart: boolean): Map<number, TableEntry> {
+ const table = new Map<number, TableEntry>();
for (const line of listing.split("\n")) {
- const [pid, parent, ...name] = line.trim().split(/\s+/);
- if (Number(pid) > 0) table.set(Number(pid), { parent: Number(parent), name: name.join(" ") });
+ const fields = line.trim().split(/\s+/);
+ const started = withStart ? BigInt(fields.splice(2, 1)[0] ?? 0) : undefined;
+ const [pid, parent, ...name] = fields;
+ if (Number(pid) > 0) {
+ table.set(Number(pid), { parent: Number(parent), name: name.join(" "), started });
+ }
}
return table;
}
-export function testProcessDescendants(parentPid: number, named?: RegExp): number[] {
- const table = processTable();
+export function ownedDescendants(table: Map<number, TableEntry>, parentPid: number): number[] {
const owned = new Set([parentPid]);
for (const pid of owned) {
- for (const [child, entry] of table) if (entry.parent === pid) owned.add(child);
+ const parentStart = table.get(pid)?.started;
+ for (const [child, entry] of table) {
+ const adopted =
+ parentStart !== undefined && entry.started !== undefined && entry.started < parentStart;
+ if (entry.parent === pid && !adopted) owned.add(child);
+ }
}
- return [...owned].filter(
- (pid) => pid !== parentPid && (!named || named.test(table.get(pid)?.name ?? "")),
+ owned.delete(parentPid);
+ return [...owned];
+}
+
+export function testProcessDescendants(parentPid: number, named?: RegExp): number[] {
+ const table = processTable();
+ return ownedDescendants(table, parentPid).filter(
+ (pid) => !named || named.test(table.get(pid)?.name ?? ""),
);
}