mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
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:
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user