fix(process): retain ownership after incomplete cleanup

This commit is contained in:
morluto
2026-10-09 02:37:47 +08:00
parent be783efaba
commit e2fc2493b6
2 changed files with 31 additions and 9 deletions
+18 -4
View File
@@ -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<ProviderProcessStopResult> {
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<ProviderProcessStopResult> {
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<ProviderProcessStopResult> {
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();
}
}
+13 -5
View File
@@ -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);
}