From b22d8170736fe94bc679b6859ad9841e0ab2092f Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 11 Oct 2026 01:59:30 +0800 Subject: [PATCH] perf(process): batch ownership tokens during rooted cleanup Use the host bulk token reader for each cleanup validation while preserving identity checks before and after token reads. Fall back for unsupported readers or omitted rows, retain explicit unavailable observations, and preserve batch and individual failures. Source ownership regressions and real owned-process boundary tests pass; a warm Darwin validation probe reduced median time from 90 ms to 52 ms. --- src/process/ProcessOwnership.ts | 78 ++++++++---- .../ProcessOwnership.validation.test.ts | 119 ++++++++++++++++++ 2 files changed, 174 insertions(+), 23 deletions(-) diff --git a/src/process/ProcessOwnership.ts b/src/process/ProcessOwnership.ts index e0330b916..90a317e92 100644 --- a/src/process/ProcessOwnership.ts +++ b/src/process/ProcessOwnership.ts @@ -641,29 +641,7 @@ const processOwnershipFailures = async ( } } } - for (const member of members) { - try { - if ((await host.environment(member.pid)).REA_PROCESS_RUN_ID !== runId) - failures.push({ pid: member.pid, reason: "run-token-mismatch" }); - } catch (cause: unknown) { - try { - const live = liveProcesses(await host.listProcesses()); - if (!live.some(({ pid }) => pid === member.pid)) continue; - } catch (recheckCause: unknown) { - failures.push({ - pid: member.pid, - reason: "environment-unreadable", - diagnostic: `${errorMessage(cause)}; process liveness recheck failed: ${errorMessage(recheckCause)}`, - }); - continue; - } - failures.push({ - pid: member.pid, - reason: "environment-unreadable", - diagnostic: errorMessage(cause), - }); - } - } + failures.push(...(await processRunTokenFailures(members, runId, host))); if ( failures.length === 0 && expectedIdentities.size > 0 && @@ -694,6 +672,60 @@ const processOwnershipFailures = async ( return failures; }; +const processRunTokenFailures = async ( + members: readonly ProcessTableEntry[], + runId: string, + host: ProcessOwnershipHost, +): Promise => { + const failures: ProcessOwnershipValidationFailure[] = []; + let runTokenObservations: + | ReadonlyMap + | undefined; + let runTokenReadFailure: string | undefined; + if (host.runTokens !== undefined) { + try { + runTokenObservations = await host.runTokens(members); + } catch (cause: unknown) { + runTokenReadFailure = errorMessage(cause); + } + } + for (const member of members) { + const batchObservation = runTokenObservations?.get(member.pid); + let observation = + batchObservation ?? (await readEnvironmentRunToken(host, member.pid)); + if ( + runTokenReadFailure !== undefined && + observation.state === "unavailable" + ) + observation = { + state: "unavailable", + reason: `${observation.reason}; run-token batch failed: ${runTokenReadFailure}`, + }; + if (observation.state === "readable") { + if (observation.runId !== runId) + failures.push({ pid: member.pid, reason: "run-token-mismatch" }); + continue; + } + try { + const live = liveProcesses(await host.listProcesses()); + if (!live.some(({ pid }) => pid === member.pid)) continue; + } catch (recheckCause: unknown) { + failures.push({ + pid: member.pid, + reason: "environment-unreadable", + diagnostic: `${observation.reason}; process liveness recheck failed: ${errorMessage(recheckCause)}`, + }); + continue; + } + failures.push({ + pid: member.pid, + reason: "environment-unreadable", + diagnostic: observation.reason, + }); + } + return failures; +}; + interface TokenOwnedProcessScan { readonly owned: readonly ProcessTableEntry[]; readonly failures: readonly ProcessOwnershipValidationFailure[]; diff --git a/src/process/ProcessOwnership.validation.test.ts b/src/process/ProcessOwnership.validation.test.ts index 29abdf876..2d6dd7275 100644 --- a/src/process/ProcessOwnership.validation.test.ts +++ b/src/process/ProcessOwnership.validation.test.ts @@ -278,6 +278,125 @@ describe("owned process-group cleanup validation: exited members", () => { }); }); +describe("owned process-group cleanup token batches", () => { + const root = { + pid: 100, + parentPid: 1, + processGroupId: 100, + state: "S", + command: "fixture", + }; + + it("batches validation and falls back only for a missing member row", async () => { + const child = { ...root, pid: 101, parentPid: root.pid }; + const runTokens = vi.fn(() => + Promise.resolve( + new Map([ + [root.pid, { state: "readable" as const, runId: ownership.runId }], + ]), + ), + ); + const environment = vi.fn(() => + Promise.resolve({ REA_PROCESS_RUN_ID: ownership.runId }), + ); + const signalGroup = vi.fn(); + const adapter: ProcessOwnershipHost = { + listProcesses: () => Promise.resolve([root, child]), + environment, + runTokens, + signalGroup, + }; + + await expect(cleanupOwnedProcessGroup(ownership, adapter)).resolves.toEqual( + { cleaned: true, signaled: true }, + ); + expect(runTokens).toHaveBeenCalledTimes(2); + expect(runTokens).toHaveBeenNthCalledWith(1, [root, child]); + expect(environment.mock.calls).toEqual([[child.pid], [child.pid]]); + expect(signalGroup).toHaveBeenCalledWith(root.processGroupId, "SIGKILL"); + }); + + it("keeps explicit unreadable rows fail-closed without environment fallback", async () => { + const environment = vi.fn(() => + Promise.resolve({ REA_PROCESS_RUN_ID: ownership.runId }), + ); + const signalGroup = vi.fn(); + const adapter: ProcessOwnershipHost = { + listProcesses: () => Promise.resolve([root]), + environment, + runTokens: () => + Promise.resolve( + new Map([ + [ + root.pid, + { state: "unavailable" as const, reason: "fixture unreadable" }, + ], + ]), + ), + signalGroup, + }; + + await expect( + cleanupOwnedProcessGroup(ownership, adapter), + ).resolves.toMatchObject({ + cleaned: false, + failures: [ + { + pid: root.pid, + reason: "environment-unreadable", + diagnostic: "fixture unreadable", + }, + ], + }); + expect(environment).not.toHaveBeenCalled(); + expect(signalGroup).not.toHaveBeenCalled(); + }); + + it("falls back to the environment reader when the batch operation throws", async () => { + const environment = vi.fn(() => + Promise.resolve({ REA_PROCESS_RUN_ID: ownership.runId }), + ); + const signalGroup = vi.fn(); + const adapter: ProcessOwnershipHost = { + listProcesses: () => Promise.resolve([root]), + environment, + runTokens: () => Promise.reject(new Error("batch reader unavailable")), + signalGroup, + }; + + await expect(cleanupOwnedProcessGroup(ownership, adapter)).resolves.toEqual( + { cleaned: true, signaled: true }, + ); + expect(environment).toHaveBeenCalledTimes(2); + expect(signalGroup).toHaveBeenCalledWith(root.processGroupId, "SIGKILL"); + }); + + it("retains both failures when batch and individual token reads fail", async () => { + const signalGroup = vi.fn(); + const adapter: ProcessOwnershipHost = { + listProcesses: () => Promise.resolve([root]), + environment: () => Promise.reject(new Error("individual unreadable")), + runTokens: () => Promise.reject(new Error("batch reader unavailable")), + signalGroup, + }; + + await expect( + cleanupOwnedProcessGroup(ownership, adapter), + ).resolves.toMatchObject({ + cleaned: false, + failures: [ + { + pid: root.pid, + reason: "environment-unreadable", + diagnostic: + "individual unreadable; run-token batch failed: batch reader unavailable", + }, + ], + }); + expect(signalGroup).not.toHaveBeenCalled(); + }); +}); + describe("Windows P0 process-tree cleanup", () => { it("reports whether taskkill signaled or found an exited tree", async () => { const terminated: WindowsProcessTreeHost = {