From cbe3d16e4459bd030f1727e9fd22ace763c3b992 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:22:18 -0500 Subject: [PATCH] test(process): join descriptor inspection and consolidate ownership proof (#12891) --- ...ssOwnership.descriptorCancellation.test.ts | 93 ++++++++++--------- ...rocessOwnership.inspectionFailures.test.ts | 82 +++++++--------- .../ProcessOwnership.validation.test.ts | 3 +- 3 files changed, 86 insertions(+), 92 deletions(-) diff --git a/src/process/ProcessOwnership.descriptorCancellation.test.ts b/src/process/ProcessOwnership.descriptorCancellation.test.ts index d24a519215..296b083b17 100644 --- a/src/process/ProcessOwnership.descriptorCancellation.test.ts +++ b/src/process/ProcessOwnership.descriptorCancellation.test.ts @@ -118,7 +118,6 @@ it("joins a pending successful native read before reporting explicit cancellatio const { rows, host } = await prepare(); const entered = createDeferred(); const release = createDeferred(); - onTestFinished(() => release.resolve()); const completed = createDeferred(); fixture.onClosed = (path) => { if (path === "/proc/44/stat") completed.resolve(); @@ -140,20 +139,24 @@ it("joins a pending successful native read before reporting explicit cancellatio .finally(() => { settled = true; }); - await awaitAcknowledgement(entered.promise, pending, "identity read"); - await awaitAcknowledgement( - completed.promise, - pending, - "sibling identity close", - ); - // Let the completed sibling publish its parsed identity after descriptor close. - await new Promise((resolve) => setImmediate(resolve)); - controller.abort(reason); - await new Promise((resolve) => setImmediate(resolve)); - expect(settled).toBe(false); - expect(fixture.handles.get("/proc/42/stat")?.fd).toBeGreaterThanOrEqual(0); - release.resolve(); - const failure = await pending; + let failure: unknown; + try { + await awaitAcknowledgement(entered.promise, pending, "identity read"); + await awaitAcknowledgement( + completed.promise, + pending, + "sibling identity close", + ); + // Let the completed sibling publish its parsed identity after descriptor close. + await new Promise((resolve) => setImmediate(resolve)); + controller.abort(reason); + await new Promise((resolve) => setImmediate(resolve)); + expect(settled).toBe(false); + expect(fixture.handles.get("/proc/42/stat")?.fd).toBeGreaterThanOrEqual(0); + } finally { + release.resolve(); + failure = await pending; + } expect(failure).toBeInstanceOf(ProcessOwnershipIdentityBatchFailure); if (!(failure instanceof ProcessOwnershipIdentityBatchFailure)) throw failure; expect(failure.origin).toBe("cancelled"); @@ -179,7 +182,6 @@ it("drains mixed identity outcomes and transfers every exact failed-close owner fixture.denied.set("/proc/43/stat", secondClose); const entered = createDeferred(); const release = createDeferred(); - onTestFinished(() => release.resolve()); fixture.onRead = async (path) => { if (path === "/proc/43/stat") { entered.resolve(); @@ -192,11 +194,15 @@ it("drains mixed identity outcomes and transfers every exact failed-close owner .finally(() => { settled = true; }); - await awaitAcknowledgement(entered.promise, pending, "identity read"); - await new Promise((resolve) => setImmediate(resolve)); - expect(settled).toBe(false); - release.resolve(); - const failure = await pending; + let failure: unknown; + try { + await awaitAcknowledgement(entered.promise, pending, "identity read"); + await new Promise((resolve) => setImmediate(resolve)); + expect(settled).toBe(false); + } finally { + release.resolve(); + failure = await pending; + } expect(failure).toBeInstanceOf(ProcessOwnershipIdentityBatchFailure); if (!(failure instanceof ProcessOwnershipIdentityBatchFailure)) throw failure; expect(failure.observations.get(44)).toMatchObject({ @@ -235,10 +241,6 @@ it("keeps an actual physical read primary alongside a sibling's explicit cancell const siblingStarted = createDeferred(); const completed = createDeferred(); const observedFailure = createDeferred(); - onTestFinished(() => { - physical.resolve(); - sibling.resolve(); - }); fixture.onClosed = (path) => { if (path === "/proc/44/stat") completed.resolve(); }; @@ -261,24 +263,29 @@ it("keeps an actual physical read primary alongside a sibling's explicit cancell host, controller.signal, ).catch((cause: unknown) => cause); - await awaitAcknowledgement( - completed.promise, - pending, - "sibling identity close", - ); - await awaitAcknowledgement( - siblingStarted.promise, - pending, - "sibling identity read", - ); - physical.resolve(); - await awaitAcknowledgement( - observedFailure.promise, - pending, - "physical read failure", - ); - sibling.resolve(); - const failure = await pending; + let failure: unknown; + try { + await awaitAcknowledgement( + completed.promise, + pending, + "sibling identity close", + ); + await awaitAcknowledgement( + siblingStarted.promise, + pending, + "sibling identity read", + ); + physical.resolve(); + await awaitAcknowledgement( + observedFailure.promise, + pending, + "physical read failure", + ); + } finally { + physical.resolve(); + sibling.resolve(); + failure = await pending; + } expect(failure).toBeInstanceOf(ProcessOwnershipIdentityBatchFailure); if (!(failure instanceof ProcessOwnershipIdentityBatchFailure)) throw failure; expect(nativeFailure).toMatchObject({ code: "EISDIR" }); diff --git a/src/process/ProcessOwnership.inspectionFailures.test.ts b/src/process/ProcessOwnership.inspectionFailures.test.ts index a2c0566370..f0e70a1909 100644 --- a/src/process/ProcessOwnership.inspectionFailures.test.ts +++ b/src/process/ProcessOwnership.inspectionFailures.test.ts @@ -20,15 +20,16 @@ const stages = [ ] as const; type Stage = (typeof stages)[number]; +const row: ProcessTableEntry = { + pid: 100, + parentPid: 1, + processGroupId: 100, + state: "S", + command: "fixture", +}; + /** Two synthetic groups only; no native host, process launch or OS signal port. */ const replay = (stage: Stage, failure: unknown) => { - const row: ProcessTableEntry = { - pid: 100, - parentPid: 1, - processGroupId: 100, - state: "S", - command: "fixture", - }; const other: ProcessTableEntry = { ...row, pid: 200, processGroupId: 200 }; const before: readonly (readonly ProcessTableEntry[])[] = stage === "initial table" @@ -48,14 +49,14 @@ const replay = (stage: Stage, failure: unknown) => { ...(sweep ? { sweepTokenOwnedProcesses: true } : {}), }; let reads = 0; - const listProcesses = vi.fn(async () => { + const listProcesses = async () => { const step = reads++; if (step < before.length) return before[step]!; if (step === before.length) throw failure; if (step > before.length + 5) throw new Error("recording table exhausted"); return []; - }); - const environment = vi.fn(async (pid: number) => { + }; + const environment = async (pid: number) => { expect([100, 200]).toContain(pid); if ( stage === "exit refresh confirmation" || @@ -67,7 +68,7 @@ const replay = (stage: Stage, failure: unknown) => { { code: "EIO" }, ); return { REA_PROCESS_RUN_ID: ownership.runId }; - }); + }; const signalGroup = vi.fn((group: number, signal: NodeJS.Signals) => { expect(stage).toBe("final verification"); expect(group).toBe(100); @@ -79,7 +80,7 @@ const replay = (stage: Stage, failure: unknown) => { environment, signalGroup, }; - return { host, ownership, signalGroup, listProcesses }; + return { host, ownership, signalGroup }; }; it.each(stages)( @@ -110,6 +111,7 @@ it.each(stages)( "keeps the ordinary EIO policy at %s without adopting an opaque cause", async (stage) => { const descriptor = await failedInspectionDescriptor(); + const closes = descriptor.closes(); const failure = Object.assign( new Error("recorded table EIO", { cause: descriptor.failure }), { code: "EIO" }, @@ -128,6 +130,7 @@ it.each(stages)( expect(recorded.signalGroup).toHaveBeenCalledTimes( stage === "final verification" ? 1 : 0, ); + expect(descriptor.closes()).toBe(closes); expect(descriptor.handle.fd).toBeGreaterThanOrEqual(0); } finally { descriptor.allow(); @@ -136,40 +139,23 @@ it.each(stages)( }, ); -it.each(["absent", "unverifiable"] as const)( - "keeps the ordinary %s process policy without native inspection or signals", - async (state) => { - const signalGroup = vi.fn(() => { - throw new Error("unproven group must not be signalled"); - }); - const host: ProcessOwnershipHost = { - platform: "linux", - listProcesses: async () => - state === "absent" - ? [] - : [ - { - pid: 100, - parentPid: 1, - processGroupId: 100, - state: "S", - command: "fixture", - }, - ], - environment: async () => { - throw Object.assign(new Error("recorded unavailable token"), { - code: "ESRCH", - }); - }, - signalGroup, - }; - const outcome = await cleanupOwnedProcessGroup(createOwnership(), host); - expect(outcome.cleaned).toBe(state === "absent"); - if (state === "unverifiable") - expect(outcome).toMatchObject({ - cleaned: false, - failures: [{ pid: 100, reason: "environment-unreadable" }], +it("refuses cleanup when a live member's token is unverifiable", async () => { + const signalGroup = vi.fn(); + const host: ProcessOwnershipHost = { + platform: "linux", + listProcesses: async () => [row], + environment: async () => { + throw Object.assign(new Error("recorded unavailable token"), { + code: "ESRCH", }); - expect(signalGroup).not.toHaveBeenCalled(); - }, -); + }, + signalGroup, + }; + expect(await cleanupOwnedProcessGroup(createOwnership(), host)).toMatchObject( + { + cleaned: false, + failures: [{ pid: 100, reason: "environment-unreadable" }], + }, + ); + expect(signalGroup).not.toHaveBeenCalled(); +}); diff --git a/src/process/ProcessOwnership.validation.test.ts b/src/process/ProcessOwnership.validation.test.ts index 3c2f11434a..388c7662a4 100644 --- a/src/process/ProcessOwnership.validation.test.ts +++ b/src/process/ProcessOwnership.validation.test.ts @@ -1158,11 +1158,12 @@ describe("owned process-group cleanup validation: exited members", () => { expect(environment).not.toHaveBeenCalled(); }); it("is idempotent when the owned group has already exited", async () => { - const { adapter } = host({}); + const { adapter, signalGroup } = host({}); expect(await cleanupOwnedProcessGroup(ownership, adapter)).toEqual({ cleaned: true, signaled: false, }); + expect(signalGroup).not.toHaveBeenCalled(); }); it("fails closed when the launcher command identity changes", async () => { const { adapter, signalGroup } = host({