Skip to content

Commit c48f6ae

Browse files
committed
fix(qa): resolve process-group liveness after Linux reaping
Recheck kernel existence when a Linux proc snapshot cannot decide liveness, and accept vanished proc handles alongside vanished paths. This prevents an already-stopped group from appearing alive again during zombie reaping. Use the canonical idempotent group signal helper in lifecycle fixture cleanup, preserving permission failures and confirmed-termination checks. Failed replacement ownership and retry-stop behavior remain unchanged. Verified with a deterministic real Linux reaper before/after probe, failing pre-fix race regressions, 129 owner/sibling tests, the full 220-file QA shard (3046 tests), three lifetime-file reruns, and complete-delta P0 autoreview.
1 parent b4833bf commit c48f6ae

4 files changed

Lines changed: 85 additions & 14 deletions

File tree

‎extensions/qa-lab/src/gateway-child-lifecycle.test.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from "node:fs/promises";
33
import path from "node:path";
44
import { afterEach, describe, expect, it, vi } from "vitest";
55
import { createQaGatewayChild } from "./gateway-child.js";
6-
import { isQaPosixProcessGroupAlive } from "./posix-process-group.js";
6+
import { isQaPosixProcessGroupAlive, signalQaPosixProcessGroup } from "./posix-process-group.js";
77
import { createTempDirHarness } from "./temp-dir.test-helper.js";
88

99
const boundary = vi.hoisted(() => ({ create: vi.fn() }));
@@ -23,15 +23,17 @@ const groups: number[] = [];
2323
afterEach(async () => {
2424
vi.restoreAllMocks();
2525
boundary.create.mockReset();
26-
for (const owner of owners.splice(0)) {
26+
for (const owner of owners) {
2727
await owner.stop();
2828
}
29-
for (const group of groups.splice(0)) {
29+
for (const group of groups) {
3030
if (isQaPosixProcessGroupAlive(group)) {
31-
process.kill(-group, "SIGKILL");
31+
expect(signalQaPosixProcessGroup(group, "SIGKILL")).toBeUndefined();
3232
}
3333
await vi.waitFor(() => expect(isQaPosixProcessGroupAlive(group)).toBe(false));
3434
}
35+
owners.length = 0;
36+
groups.length = 0;
3537
await dirs.cleanup();
3638
});
3739

‎extensions/qa-lab/src/gateway-startup-lease.integration.test.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { fileURLToPath } from "node:url";
77
import { describe, expect, it, vi } from "vitest";
88
import { createQaBusState } from "./bus-state.js";
99
import type { QaLabServerHandle } from "./lab-server.types.js";
10-
import { isQaPosixProcessGroupAlive } from "./posix-process-group.js";
10+
import { isQaPosixProcessGroupAlive, signalQaPosixProcessGroup } from "./posix-process-group.js";
1111
import type { QaTransportAdapterFactory } from "./qa-transport-registry.js";
1212
import { runQaFlowSuiteStandard } from "./suite-run-standard.js";
1313
import type { QaSuiteResolvedRunContext } from "./suite-types.js";
@@ -320,11 +320,9 @@ async function reproduce(denyGroupSignals: boolean) {
320320
const identity = readIdentity(root);
321321
if (identity && alive(-identity.pgid)) {
322322
record("diagnostic-force-kill", snapshot());
323-
realKill(-identity.pgid, "SIGKILL");
323+
expect(signalQaPosixProcessGroup(identity.pgid, "SIGKILL")).toBeUndefined();
324324
}
325325
const deadline = Date.now() + 5_000;
326-
// Retain the settled observation: a later Linux probe may see no /proc
327-
// members during reaping and conservatively report an unknown group alive.
328326
cleaned = snapshot();
329327
while (cleaned.groupAlive && Date.now() < deadline) {
330328
await sleep(25);

‎extensions/qa-lab/src/posix-process-group.test.ts‎

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
import { afterEach, describe, expect, it, vi } from "vitest";
2-
import { isQaPosixProcessGroupAlive, signalQaPosixProcessGroup } from "./posix-process-group.js";
2+
import {
3+
inspectLinuxProcessGroup,
4+
isQaPosixProcessGroupAlive,
5+
signalQaPosixProcessGroup,
6+
} from "./posix-process-group.js";
37
import { inspectLinuxProcessGroupStats } from "./posix-process-stat.js";
48

9+
const procFs = vi.hoisted(() => ({ readFileSync: vi.fn(), readdirSync: vi.fn() }));
10+
vi.mock("node:fs", async (importOriginal) => {
11+
const actual = await importOriginal<typeof import("node:fs")>();
12+
return {
13+
...actual,
14+
readFileSync: procFs.readFileSync.mockImplementation(actual.readFileSync),
15+
readdirSync: procFs.readdirSync.mockImplementation(actual.readdirSync),
16+
};
17+
});
18+
519
afterEach(() => {
620
vi.restoreAllMocks();
721
});
@@ -47,6 +61,50 @@ describe("POSIX process group inspection", () => {
4761
expect(inspection.diagnostics).toMatch(/\.\.\.$/u);
4862
});
4963

64+
it.each(["ENOENT", "ESRCH", "EACCES"])(
65+
"distinguishes a vanished /proc member from unreadable state (%s)",
66+
(code) => {
67+
vi.spyOn(process, "platform", "get").mockReturnValue("linux");
68+
procFs.readdirSync.mockReturnValueOnce([
69+
{ name: "123", isDirectory: () => true },
70+
{ name: "999", isDirectory: () => true },
71+
]);
72+
procFs.readFileSync
73+
.mockReturnValueOnce("123 (leader) Z 1 123 123 0 -1 0")
74+
.mockImplementationOnce(() => {
75+
throw Object.assign(new Error("stat read failed"), { code });
76+
});
77+
78+
const inspection = inspectLinuxProcessGroup(123);
79+
if (code === "EACCES") {
80+
expect(inspection).toBeNull();
81+
} else {
82+
expect(inspection?.alive).toBe(false);
83+
}
84+
},
85+
);
86+
87+
it.each(["empty", "unavailable"])(
88+
"confirms a group reaped during an %s Linux snapshot is stopped",
89+
(snapshot) => {
90+
vi.spyOn(process, "platform", "get").mockReturnValue("linux");
91+
let reaped = false;
92+
vi.spyOn(process, "kill").mockImplementation(() => {
93+
if (reaped) {
94+
throw Object.assign(new Error("group reaped"), { code: "ESRCH" });
95+
}
96+
return true;
97+
});
98+
99+
expect(
100+
isQaPosixProcessGroupAlive(123, () => {
101+
reaped = true;
102+
return snapshot === "empty" ? inspectLinuxProcessGroupStats(123, []) : null;
103+
}),
104+
).toBe(false);
105+
},
106+
);
107+
50108
it("fails closed when the Linux member snapshot is unavailable", () => {
51109
const platform = vi.spyOn(process, "platform", "get").mockReturnValue("linux");
52110
const processKill = vi.spyOn(process, "kill").mockImplementation(() => true);
@@ -87,4 +145,14 @@ describe("POSIX process group inspection", () => {
87145
expect(signalQaPosixProcessGroup(123, "SIGTERM")).toBeUndefined();
88146
expect(processKill).not.toHaveBeenCalledWith(123, expect.anything());
89147
});
148+
149+
it.each(["ESRCH", "EPERM"])("preserves the group signal contract on %s", (code) => {
150+
const failure = Object.assign(new Error("group signal failed"), { code });
151+
const processKill = vi.spyOn(process, "kill").mockImplementation(() => {
152+
throw failure;
153+
});
154+
155+
expect(signalQaPosixProcessGroup(123, "SIGKILL")).toBe(code === "ESRCH" ? undefined : failure);
156+
expect(processKill.mock.calls).toEqual([[-123, "SIGKILL"]]);
157+
});
90158
});

‎extensions/qa-lab/src/posix-process-group.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export function inspectLinuxProcessGroup(
2727
try {
2828
stats.push(readFileSync(path.join("/proc", entry.name, "stat"), "utf8"));
2929
} catch (error) {
30-
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
30+
// Exit can race either opening stat (ENOENT) or reading its open fd (ESRCH).
31+
if (!["ENOENT", "ESRCH"].includes((error as NodeJS.ErrnoException).code ?? "")) {
3132
return null;
3233
}
3334
}
@@ -41,13 +42,15 @@ export function isQaPosixProcessGroupAlive(
4142
) {
4243
try {
4344
process.kill(-processGroupId, 0);
45+
// Reaping can remove the last zombie between kill(0) and /proc. Resolve an
46+
// inconclusive snapshot with a fresh existence probe, never the stale one.
47+
return (
48+
process.platform !== "linux" ||
49+
(inspectLinuxProcessGroupFn(processGroupId)?.alive ?? process.kill(-processGroupId, 0))
50+
);
4451
} catch (error) {
4552
return (error as NodeJS.ErrnoException).code !== "ESRCH";
4653
}
47-
if (process.platform !== "linux") {
48-
return true;
49-
}
50-
return inspectLinuxProcessGroupFn(processGroupId)?.alive ?? true;
5154
}
5255

5356
export function signalQaPosixProcessGroup(

0 commit comments

Comments
 (0)