fix(engine): take the browser lease inside the try that releases it

The lease was acquired above the try, with `mkdtempSync` between the two — so a
failure there (a full disk, a read-only tmpdir) returned without ever reaching
the `finally` that releases it. A leaked pooled browser is worse than the error
that caused it: a pool with no leases left hangs every later render instead of
failing one.

The temp dir is now taken first and the lease inside the try, with the release
made conditional. The abort check moves above the acquire too, so an
already-cancelled track no longer takes a browser out of the pool just to hand
it straight back.

No test: the fix is the ordering of two resource acquisitions, and the only way
to observe it is to make mkdtempSync throw, which needs an injection seam that
does not exist here and would be more risk than the three lines it guards.
This commit is contained in:
Vance Ingalls
2026-08-12 00:47:44 -07:00
parent 4a3440c535
commit a27c7b684e
+11 -7
View File
@@ -214,17 +214,21 @@ export async function applyAudioFxChain(
// never costs a browser.
if ((planes[0]?.length ?? 0) === 0) return inputWav;
// Audio processing needs no GPU or special capture mode; a plain sandboxed
// browser is enough, and the lease pool reuses one across tracks.
const lease = await acquireBrowser([
"--no-sandbox",
"--autoplay-policy=no-user-gesture-required",
]);
// Both resources are taken INSIDE the try that releases them. The lease used
// to be acquired above it, with the mkdtemp between — so a failure there
// (a full disk, a read-only tmpdir) leaked a pooled browser, and a pool with
// no leases left hangs every later render rather than failing one.
const hostDir = mkdtempSync(join(tmpdir(), "hf-fx-host-"));
let lease: Awaited<ReturnType<typeof acquireBrowser>> | null = null;
try {
// Audio processing needs no GPU or special capture mode; a plain sandboxed
// browser is enough, and the lease pool reuses one across tracks.
// Checked before the lease, not after: an already-cancelled track has no
// reason to take a browser out of the pool just to hand it straight back.
if (options.signal?.aborted) {
throw new AudioFxRenderError(`Audio FX cancelled for track ${options.trackId}`);
}
lease = await acquireBrowser(["--no-sandbox", "--autoplay-policy=no-user-gesture-required"]);
const page = await lease.browser.newPage();
try {
// AudioWorklet is only exposed in a secure context, and about:blank is
@@ -310,7 +314,7 @@ export async function applyAudioFxChain(
);
} finally {
rmSync(hostDir, { recursive: true, force: true });
await lease.release().catch(() => undefined);
await lease?.release().catch(() => undefined);
}
}