fix(encoder): signal host interruptions for retry (#3578)

* fix(encoder): signal host interruptions for retry

* fix(encoder): cover all render interruption paths

* fix(encoder): classify HDR pre-extraction drains
This commit is contained in:
James Russo
2026-08-31 22:07:43 -04:00
committed by GitHub
parent 9097d539b1
commit 0f7eebd7e4
35 changed files with 604 additions and 44 deletions
+59 -1
View File
@@ -2,7 +2,65 @@ import { EventEmitter } from "node:events";
import { resolve } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { formatFfmpegError } from "./runFfmpeg.js";
import { formatFfmpegError, isExternalFfmpegInterruption } from "./runFfmpeg.js";
describe("isExternalFfmpegInterruption", () => {
const base = {
exitCode: 255,
signal: null,
stderr: "",
terminationReason: "exit" as const,
};
it("recognizes a direct external termination signal", () => {
expect(isExternalFfmpegInterruption({ ...base, exitCode: null, signal: "SIGTERM" })).toBe(true);
});
it("does not broaden the retry signal to SIGKILL", () => {
expect(isExternalFfmpegInterruption({ ...base, exitCode: null, signal: "SIGKILL" })).toBe(
false,
);
});
it("recognizes ffmpeg's handled SIGTERM exit-255 signature", () => {
expect(
isExternalFfmpegInterruption({
...base,
stderr: "frame= 42\nExiting normally, received signal 15.\n",
}),
).toBe(true);
});
it("does not classify an ordinary exit 255", () => {
expect(isExternalFfmpegInterruption({ ...base, stderr: "Encoder initialization failed" })).toBe(
false,
);
});
it("requires exit 255 when classification relies on ffmpeg stderr", () => {
expect(
isExternalFfmpegInterruption({
...base,
exitCode: 1,
stderr: "Exiting normally, received signal 15.",
}),
).toBe(false);
});
it.each(["abort", "deadline", "inactivity"] as const)(
"keeps a managed %s termination non-retryable",
(terminationReason) => {
expect(
isExternalFfmpegInterruption({
...base,
signal: "SIGTERM",
stderr: "Exiting normally, received signal 15.",
terminationReason,
}),
).toBe(false);
},
);
});
describe("formatFfmpegError", () => {
const originalPlatform = process.platform;
+26 -1
View File
@@ -23,12 +23,32 @@ export interface RunFfmpegOptions {
export interface RunFfmpegResult {
success: boolean;
exitCode: number | null;
signal?: NodeJS.Signals | null;
stderr: string;
durationMs: number;
terminationReason: ManagedProcessTerminationReason;
failureReason?: "external_interruption";
error?: Error;
}
const FFMPEG_SIGTERM_EXIT_LINE = /^Exiting normally, received signal 15\.?\r?$/m;
/**
* Return true only when ffmpeg was terminated from outside this managed call.
*
* FFmpeg handles SIGTERM itself and can therefore report exit code 255 with a
* null Node signal. The exact terminal stderr line covers that case. Managed
* abort/deadline/inactivity reasons always take precedence so our own SIGTERM
* requests never become retryable lifecycle interruptions.
*/
export function isExternalFfmpegInterruption(
result: Pick<RunFfmpegResult, "exitCode" | "signal" | "stderr" | "terminationReason">,
): boolean {
if (result.terminationReason !== "exit" || result.exitCode === 0) return false;
if (result.signal === "SIGTERM") return true;
return result.exitCode === 255 && FFMPEG_SIGTERM_EXIT_LINE.test(result.stderr);
}
const DEFAULT_TIMEOUT = 300_000;
const DEFAULT_STDERR_TAIL_LINES = 15;
@@ -104,12 +124,17 @@ export async function runFfmpeg(args: string[], opts?: RunFfmpegOptions): Promis
onStderr: opts?.onStderr,
});
const outcome = await managed.wait();
return {
const result: RunFfmpegResult = {
success: outcome.reason === "exit" && outcome.exitCode === 0,
exitCode: outcome.exitCode,
signal: outcome.signal,
stderr: outcome.stderr,
durationMs: outcome.durationMs,
terminationReason: outcome.reason,
error: outcome.error,
};
if (isExternalFfmpegInterruption(result)) {
result.failureReason = "external_interruption";
}
return result;
}