mirror of
https://github.com/morluto/rea.git
synced 2026-11-18 20:53:00 +00:00
* fix(process): settle an observed exit in progress before reporting a process present An exiting process closes its sockets before it becomes a zombie. A caller that reacts to the closed transport could read the captured identity in that window and report it as present, so Hopper cleanup retained the document and private runtime although the application had exited. Wait, within the existing cleanup verification grace, only while the same identity is positively observed exiting. Refs #10556 * refactor(process): reuse exit settlement when observing a bridge exit Review follow-up for observeProcessStartIdentityExit. The exit check added its own waiting loop beside settleExitingProcessIdentities, with the same deadline, polling interval and same-identity exiting check, so future exit-race fixes would need two changes. The check now hands its first observation to the shared settlement and keeps only the final result choice. The settlement and its identity reader accept PID-only rows, the shape the host identity reader already takes. One liveProcessIdentity helper maps absent and exited identities for both observeProcessStartIdentity and the settled result. Behavior is unchanged: an exiting identity is re-read until it leaves exit in progress or the cleanup grace ends; a replacement, absent or zombie identity is exited, an unreadable one unverified, and one still exiting at the deadline present. Tests: src/process (850), the Hopper cleanup and session tests (15) and 20 Hopper boundary test files (114) pass. check:fast passes. --------- Co-authored-by: Davmunrey <Davmunrey@users.noreply.github.com>
365 lines
11 KiB
TypeScript
365 lines
11 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import type {
|
|
ProcessIdentityObservation,
|
|
ProcessOwnershipHost,
|
|
} from "./ProcessOwnership.js";
|
|
import {
|
|
observeProcessStartIdentity,
|
|
observeProcessStartIdentityExit,
|
|
signalProcessWithStartIdentity,
|
|
} from "./ProcessOwnershipObservation.js";
|
|
|
|
const host = (identity: string | undefined): ProcessOwnershipHost => ({
|
|
platform: "linux",
|
|
listProcesses: async () =>
|
|
identity === undefined
|
|
? []
|
|
: [
|
|
{
|
|
pid: 42,
|
|
parentPid: 1,
|
|
processGroupId: 42,
|
|
uid: 1000,
|
|
state: "S",
|
|
command: "/bin/fixture",
|
|
},
|
|
],
|
|
environment: async () => ({}),
|
|
processIdentities: async () =>
|
|
new Map([
|
|
[
|
|
42,
|
|
identity === undefined
|
|
? { state: "absent", reason: "not-found-in-caller-pid-namespace" }
|
|
: { state: "readable", identity },
|
|
],
|
|
]),
|
|
signalGroup: () => undefined,
|
|
});
|
|
|
|
describe("selected native process identity", () => {
|
|
it.each(["readable", "unavailable"] as const)(
|
|
"does not infer exit from a table omission when native identity is %s",
|
|
async (state) => {
|
|
const omitted: ProcessOwnershipHost = {
|
|
...host("start-1"),
|
|
listProcesses: async () => [],
|
|
processIdentities: async () =>
|
|
new Map([
|
|
[
|
|
42,
|
|
state === "readable"
|
|
? { state: "readable", identity: "start-1" }
|
|
: { state: "unavailable", reason: "native inspection denied" },
|
|
],
|
|
]),
|
|
};
|
|
await expect(
|
|
observeProcessStartIdentityExit(42, "start-1", omitted),
|
|
).resolves.toBe(state === "readable" ? "present" : "unverified");
|
|
},
|
|
);
|
|
|
|
it.each(["absent", "zombie", "dead"] as const)(
|
|
"uses native %s proof despite a stale live process-table row",
|
|
async (lifecycle) => {
|
|
const exited: ProcessOwnershipHost = {
|
|
...host("start-1"),
|
|
processIdentities: async () =>
|
|
new Map([
|
|
[
|
|
42,
|
|
lifecycle === "absent"
|
|
? {
|
|
state: "absent",
|
|
reason: "not-found-in-caller-pid-namespace",
|
|
}
|
|
: {
|
|
state: "readable",
|
|
identity: "start-1",
|
|
lifecycle: { state: "observed", lifecycle },
|
|
},
|
|
],
|
|
]),
|
|
};
|
|
await expect(
|
|
observeProcessStartIdentityExit(42, "start-1", exited),
|
|
).resolves.toBe("exited");
|
|
const sendSignal = vi.fn();
|
|
await expect(
|
|
signalProcessWithStartIdentity(42, "start-1", "SIGKILL", {
|
|
host: exited,
|
|
sendSignal,
|
|
}),
|
|
).resolves.toBe("gone");
|
|
expect(sendSignal).not.toHaveBeenCalled();
|
|
},
|
|
);
|
|
|
|
it("reports an observed exit in progress without confirming exit", async () => {
|
|
const exiting: ProcessOwnershipHost = {
|
|
...host("start-1"),
|
|
processIdentities: async () =>
|
|
new Map([
|
|
[
|
|
42,
|
|
{
|
|
state: "readable",
|
|
identity: "start-1",
|
|
lifecycle: { state: "observed", lifecycle: "exiting" },
|
|
},
|
|
],
|
|
]),
|
|
};
|
|
await expect(
|
|
observeProcessStartIdentityExit(42, "start-1", exiting),
|
|
).resolves.toBe("exiting");
|
|
});
|
|
|
|
it("binds an omitted native PID to its directly observed run token", async () => {
|
|
const omitted: ProcessOwnershipHost = {
|
|
...host("start-1"),
|
|
listProcesses: async () => [],
|
|
environment: async () => ({ REA_PROCESS_RUN_ID: "captured-run" }),
|
|
};
|
|
await expect(
|
|
observeProcessStartIdentity(42, omitted, "captured-run"),
|
|
).resolves.toMatchObject({ state: "readable", identity: "start-1" });
|
|
});
|
|
|
|
it.each(["unreadable", "foreign", "reused"] as const)(
|
|
"does not bind an omitted PID with a %s token observation",
|
|
async (condition) => {
|
|
let identity = "start-1";
|
|
const omitted: ProcessOwnershipHost = {
|
|
...host("start-1"),
|
|
listProcesses: async () => [],
|
|
processIdentities: async () =>
|
|
new Map([[42, { state: "readable", identity }]]),
|
|
environment: async () => {
|
|
if (condition === "unreadable") throw new Error("environment denied");
|
|
if (condition === "reused") identity = "replacement-start";
|
|
return {
|
|
REA_PROCESS_RUN_ID:
|
|
condition === "foreign" ? "other-run" : "captured-run",
|
|
};
|
|
},
|
|
};
|
|
await expect(
|
|
observeProcessStartIdentity(42, omitted, "captured-run"),
|
|
).resolves.toMatchObject({ state: "unavailable" });
|
|
},
|
|
);
|
|
});
|
|
|
|
describe("process start identity lease", () => {
|
|
it("captures a readable identity for a live PID", async () => {
|
|
await expect(
|
|
observeProcessStartIdentity(42, host("start-1")),
|
|
).resolves.toEqual({
|
|
state: "readable",
|
|
identity: "start-1",
|
|
});
|
|
});
|
|
|
|
it("confirms exit or PID replacement without treating unreadable state as exit", async () => {
|
|
await expect(
|
|
observeProcessStartIdentityExit(42, "start-1", host(undefined)),
|
|
).resolves.toBe("exited");
|
|
await expect(
|
|
observeProcessStartIdentityExit(42, "start-1", host("replacement-start")),
|
|
).resolves.toBe("exited");
|
|
await expect(
|
|
observeProcessStartIdentityExit(42, "start-1", host("start-1")),
|
|
).resolves.toBe("present");
|
|
const unavailable: ProcessOwnershipHost = {
|
|
...host("start-1"),
|
|
processIdentities: async () =>
|
|
new Map([[42, { state: "unavailable", reason: "reader unavailable" }]]),
|
|
};
|
|
await expect(
|
|
observeProcessStartIdentityExit(42, "start-1", unavailable),
|
|
).resolves.toBe("unverified");
|
|
});
|
|
|
|
it("does not adopt a replacement PID's first identity for an owned launch", async () => {
|
|
const replacement: ProcessOwnershipHost = {
|
|
...host("replacement-start"),
|
|
environment: async () => ({ REA_PROCESS_RUN_ID: "another-run" }),
|
|
};
|
|
await expect(
|
|
observeProcessStartIdentity(42, replacement, "captured-run"),
|
|
).resolves.toMatchObject({
|
|
state: "unavailable",
|
|
reason: expect.stringContaining("ownership"),
|
|
});
|
|
});
|
|
|
|
it("does not establish an owned launch when its token is unreadable", async () => {
|
|
const unreadable: ProcessOwnershipHost = {
|
|
...host("start-1"),
|
|
runTokens: async () =>
|
|
new Map([
|
|
[42, { state: "unavailable", reason: "environment_errno_EACCES" }],
|
|
]),
|
|
};
|
|
await expect(
|
|
observeProcessStartIdentity(42, unreadable, "captured-run"),
|
|
).resolves.toMatchObject({
|
|
state: "unavailable",
|
|
reason: expect.stringContaining("environment_errno_EACCES"),
|
|
});
|
|
});
|
|
|
|
it("rejects an identity that changes while the captured run token is read", async () => {
|
|
let identity = "start-1";
|
|
const changing: ProcessOwnershipHost = {
|
|
...host("start-1"),
|
|
processIdentities: async () =>
|
|
new Map([[42, { state: "readable", identity }]]),
|
|
environment: async () => {
|
|
identity = "replacement-start";
|
|
return { REA_PROCESS_RUN_ID: "captured-run" };
|
|
},
|
|
};
|
|
await expect(
|
|
observeProcessStartIdentity(42, changing, "captured-run"),
|
|
).resolves.toMatchObject({
|
|
state: "unavailable",
|
|
reason: expect.stringContaining("identity changed"),
|
|
});
|
|
});
|
|
|
|
it("signals the PID when its launch identity still matches", async () => {
|
|
const sendSignal = vi.fn();
|
|
await expect(
|
|
signalProcessWithStartIdentity(42, "start-1", "SIGKILL", {
|
|
host: host("start-1"),
|
|
sendSignal,
|
|
}),
|
|
).resolves.toBe("signaled");
|
|
expect(sendSignal).toHaveBeenCalledWith(42, "SIGKILL");
|
|
});
|
|
|
|
it("does not signal a reused PID even when its command is unchanged", async () => {
|
|
const sendSignal = vi.fn();
|
|
await expect(
|
|
signalProcessWithStartIdentity(42, "start-1", "SIGKILL", {
|
|
host: host("start-2"),
|
|
sendSignal,
|
|
}),
|
|
).resolves.toBe("identity-changed");
|
|
expect(sendSignal).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not signal when the PID identity is unavailable", async () => {
|
|
const unavailable: ProcessOwnershipHost = {
|
|
...host("start-1"),
|
|
processIdentities: async () =>
|
|
new Map([[42, { state: "unavailable", reason: "reader unavailable" }]]),
|
|
};
|
|
const sendSignal = vi.fn();
|
|
await expect(
|
|
signalProcessWithStartIdentity(42, "start-1", "SIGKILL", {
|
|
host: unavailable,
|
|
sendSignal,
|
|
}),
|
|
).resolves.toBe("unverified");
|
|
expect(sendSignal).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("reports an already-gone PID without signaling", async () => {
|
|
const sendSignal = vi.fn();
|
|
await expect(
|
|
signalProcessWithStartIdentity(42, "start-1", "SIGKILL", {
|
|
host: host(undefined),
|
|
sendSignal,
|
|
}),
|
|
).resolves.toBe("gone");
|
|
expect(sendSignal).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("observed exit in progress", () => {
|
|
const exiting = {
|
|
state: "readable",
|
|
identity: "start-1",
|
|
lifecycle: { state: "observed", lifecycle: "exiting" },
|
|
} as const;
|
|
const sequence = (
|
|
...observations: readonly ProcessIdentityObservation[]
|
|
): { readonly host: ProcessOwnershipHost; reads(): number } => {
|
|
let reads = 0;
|
|
return {
|
|
reads: () => reads,
|
|
host: {
|
|
...host("start-1"),
|
|
processIdentities: async () => {
|
|
const observation =
|
|
observations[Math.min(reads, observations.length - 1)];
|
|
reads += 1;
|
|
if (observation === undefined) throw new Error("no observation");
|
|
return new Map([[42, observation]]);
|
|
},
|
|
},
|
|
};
|
|
};
|
|
|
|
it.each([
|
|
[
|
|
"zombie",
|
|
{
|
|
state: "readable",
|
|
identity: "start-1",
|
|
lifecycle: { state: "observed", lifecycle: "zombie" },
|
|
},
|
|
"exited",
|
|
],
|
|
[
|
|
"absent",
|
|
{ state: "absent", reason: "not-found-in-caller-pid-namespace" },
|
|
"exited",
|
|
],
|
|
[
|
|
"replaced",
|
|
{ state: "readable", identity: "replacement-start" },
|
|
"exited",
|
|
],
|
|
[
|
|
"unavailable",
|
|
{ state: "unavailable", reason: "identity read failed" },
|
|
"unverified",
|
|
],
|
|
] as const)(
|
|
"settles an observed exit in progress that becomes %s",
|
|
async (_name, settled, expected) => {
|
|
const observed = sequence(exiting, exiting, settled);
|
|
await expect(
|
|
observeProcessStartIdentityExit(42, "start-1", observed.host),
|
|
).resolves.toBe(expected);
|
|
expect(observed.reads()).toBe(3);
|
|
},
|
|
);
|
|
|
|
it("reports exiting only after waiting out the cleanup grace", async () => {
|
|
const observed = sequence(exiting);
|
|
await expect(
|
|
observeProcessStartIdentityExit(42, "start-1", observed.host),
|
|
).resolves.toBe("exiting");
|
|
expect(observed.reads()).toBeGreaterThan(1);
|
|
});
|
|
|
|
it("does not wait on a live process that is not exiting", async () => {
|
|
const observed = sequence({
|
|
state: "readable",
|
|
identity: "start-1",
|
|
lifecycle: { state: "observed", lifecycle: "non-zombie" },
|
|
});
|
|
await expect(
|
|
observeProcessStartIdentityExit(42, "start-1", observed.host),
|
|
).resolves.toBe("present");
|
|
expect(observed.reads()).toBe(1);
|
|
});
|
|
});
|