diff --git a/src/process/ProviderProcess.ts b/src/process/ProviderProcess.ts index ef5ff8262..896ae47e6 100644 --- a/src/process/ProviderProcess.ts +++ b/src/process/ProviderProcess.ts @@ -299,11 +299,14 @@ export class ProviderProcessSupervisor { ); } - /** Stop an owned process once; concurrent callers share the same escalation. */ + /** Share each stop attempt; incomplete cleanup retains ownership and permits retry. */ stop( options: ProviderProcessStopOptions = {}, ): Promise { - this.#stopPromise ??= this.#stop(options); + this.#stopPromise ??= this.#stop(options).then((result) => { + if (result.status === "incomplete") this.#stopPromise = undefined; + return result; + }); return this.#stopPromise; } @@ -320,6 +323,19 @@ export class ProviderProcessSupervisor { async #stop( options: ProviderProcessStopOptions, + ): Promise { + let completed = false; + try { + const result = await this.#stopWithin(options); + completed = result.status !== "incomplete"; + return result; + } finally { + if (completed) this.dispose(); + } + } + + async #stopWithin( + options: ProviderProcessStopOptions, ): Promise { try { if (!this.launch.ownsProcessLifetime) return { status: "not-owned" }; @@ -366,8 +382,6 @@ export class ProviderProcessSupervisor { ? cause.message : "owned provider process cleanup failed", }; - } finally { - this.dispose(); } } diff --git a/tests/boundary/process/providerProcess.test.ts b/tests/boundary/process/providerProcess.test.ts index 8e1440eff..f6143dc5c 100644 --- a/tests/boundary/process/providerProcess.test.ts +++ b/tests/boundary/process/providerProcess.test.ts @@ -325,12 +325,14 @@ describe("provider process output and cleanup primitives", () => { expect(cleanup).toHaveBeenCalled(); }); - it("reports incomplete cleanup when a verified callback leaves the child alive", async () => { + it("retains a live child after incomplete cleanup and permits a later stop", async () => { const child = spawnProviderProcessFixture("stubborn"); await waitForProviderProcessReady(child); - const cleanup = vi.fn( - async () => ({ cleaned: true, signaled: false }) as const, - ); + let release = false; + const cleanup = () => { + if (release) child.kill("SIGKILL"); + return Promise.resolve({ cleaned: true, signaled: release } as const); + }; const supervisor = new ProviderProcessSupervisor({ process: child, ownsProcessLifetime: true, @@ -341,7 +343,13 @@ describe("provider process output and cleanup primitives", () => { status: "incomplete", reason: "verified process-group cleanup did not stop the launcher", }); - expect(cleanup).toHaveBeenCalled(); + expect(child.exitCode).toBeNull(); + expect(child.signalCode).toBeNull(); + release = true; + await expect(supervisor.stop()).resolves.toEqual({ + status: "verified-cleanup", + }); + expect(supervisor.snapshot().signal).toBe("SIGKILL"); } finally { await stopProviderProcessFixture(child); }