Skip to content

Commit 86ae93d

Browse files
committed
fix(engine): stop nested lock retries from restarting an exhausted spawn
An exhausted retry error still matched the locked-spawn check, so an outer retry wrapper (detectGpuEncoder inside spawnStreamingEncoder) restarted the whole sequence. Tag exhausted errors so the classifier declines them.
1 parent 1c5fded commit 86ae93d

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎packages/engine/src/utils/spawnLockRetry.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@ describe("retrySpawnWhileLocked", () => {
6868
expect(run).toHaveBeenCalledTimes(3);
6969
});
7070

71+
it("does not restart an inner retry that already gave up", async () => {
72+
const inner = vi.fn(async (): Promise<string> => {
73+
throw spawnError("EBUSY");
74+
});
75+
const outer = vi.fn(() =>
76+
retrySpawnWhileLocked("C:/ff/ffmpeg.exe", () =>
77+
retrySpawnWhileLocked("C:/ff/ffprobe.exe", inner),
78+
),
79+
);
80+
81+
const result = outer();
82+
const settled = expect(result).rejects.toMatchObject({ code: "EBUSY" });
83+
await vi.runAllTimersAsync();
84+
85+
await settled;
86+
expect(inner).toHaveBeenCalledTimes(3);
87+
expect(outer).toHaveBeenCalledTimes(1);
88+
});
89+
7190
it.each([
7291
["EPERM, which an ACL that denies execute also raises", spawnError("EPERM")],
7392
["ENOENT", spawnError("ENOENT")],

‎packages/engine/src/utils/spawnLockRetry.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
const LOCKED_EXECUTABLE_CODES = new Set(["EBUSY", "ETXTBSY"]);
1010
const RETRY_DELAYS_MS = [250, 500];
1111

12+
// Errors this module has already given up on. An outer retry wrapper must not
13+
// treat them as a fresh lock, or nested wrappers multiply the attempts.
14+
const exhaustedErrors = new WeakSet<object>();
15+
1216
function isLockedExecutableSpawn(error: unknown): error is NodeJS.ErrnoException {
1317
return (
1418
error instanceof Error &&
19+
!exhaustedErrors.has(error) &&
1520
"syscall" in error &&
1621
error.syscall === "spawn" &&
1722
"code" in error &&
@@ -29,14 +34,16 @@ export async function retrySpawnWhileLocked<T>(binary: string, run: () => Promis
2934
if (!isLockedExecutableSpawn(error)) throw error;
3035
const delayMs = RETRY_DELAYS_MS[attempt];
3136
if (delayMs === undefined) {
32-
throw Object.assign(
37+
const exhausted = Object.assign(
3338
new Error(
3439
`[FFmpeg] Could not start "${binary}" (${error.code}) after ${attempt + 1} attempts: ` +
3540
"another process kept the executable open, often antivirus scanning it.",
3641
{ cause: error },
3742
),
3843
{ code: error.code, errno: error.errno, syscall: error.syscall },
3944
);
45+
exhaustedErrors.add(exhausted);
46+
throw exhausted;
4047
}
4148
console.warn(
4249
`[FFmpeg] "${binary}" is locked (${error.code}); retrying in ${delayMs} ms (attempt ${attempt + 2}).`,

0 commit comments

Comments
 (0)