fix(cli): stopping remove-background on a Mac no longer aborts the process (#6848)

* fix(cli): stopping remove-background on a Mac no longer aborts the process

* fix(cli): a quit after background removal released its session exits at once

* Revert "fix(cli): a quit after background removal released its session exits at once"

This reverts commit 3b9d6ffc24.
This commit is contained in:
Miguel Ángel
2026-10-31 17:08:22 +00:00
committed by GitHub
parent 1e5413f34c
commit ea7d43c616
5 changed files with 211 additions and 27 deletions
+39 -1
View File
@@ -1,9 +1,11 @@
import { afterEach, describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
CliResultSignal,
CliRuntimeError,
CliUsageError,
consumeCommandResult,
drainToExit,
endProcess,
failCommand,
failUsage,
finishCommand,
@@ -33,3 +35,39 @@ describe("command result contract", () => {
expect(consumeCommandResult()).toMatchObject({ exitCode: 1, kind: "runtime_error" });
});
});
describe("ending the process", () => {
const host = () => {
const pending: (() => void)[] = [];
const calls = { exit: vi.fn(), setExitCode: vi.fn() };
const later = ((run: () => void) => {
pending.push(run);
return { unref: () => undefined };
}) as unknown as typeof setTimeout;
return { pending, calls, ...calls, later };
};
it("exits at once when nothing native needs tearing down", async () => {
vi.resetModules();
const fresh = await import("./commandResult.js");
const ending = host();
fresh.endProcess(143, ending);
expect(ending.exit).toHaveBeenCalledWith(143);
expect(ending.pending).toHaveLength(0);
});
it("after CoreML ran, leaves the exit to Node's own teardown and forces it only after a grace", () => {
const ending = host();
drainToExit();
endProcess(143, ending);
expect(ending.exit).not.toHaveBeenCalled();
expect(ending.setExitCode).toHaveBeenCalledWith(143);
expect(ending.pending).toHaveLength(1);
ending.pending[0]!();
expect(ending.exit).toHaveBeenCalledWith(143);
});
});
+19
View File
@@ -120,6 +120,25 @@ export async function outputWritten(): Promise<void> {
await Promise.all([written(process.stdout), written(process.stderr)]);
}
let drainsToExit = false;
const DRAIN_GRACE_MS = 8_000;
export function drainToExit(): void {
drainsToExit = true;
}
export interface ProcessEnd {
exit(exitCode: number): void;
setExitCode(exitCode: number): void;
later: typeof setTimeout;
}
export function endProcess(exitCode: number, host: ProcessEnd): void {
if (!drainsToExit) return host.exit(exitCode);
host.setExitCode(exitCode);
host.later(() => host.exit(exitCode), DRAIN_GRACE_MS).unref();
}
/** Ask cli.ts to finalize telemetry/output and then terminate the process. */
export function requestCliExit(exitCode = 0): void {
if (!rootExitRequester) {