Skip to content

Write HTML. Render video. Built for agents.

fix(engine): audio duration probe reads to the end on Windows; cli tests skip bun's disk cache (#5395)

Miguel Ángel3d6de0581ed9
diff --git a/packages/cli/scripts/test-home.mjs b/packages/cli/scripts/test-home.mjs
index f89bf0df6b..708c7ac013 100644
--- a/packages/cli/scripts/test-home.mjs
+++ b/packages/cli/scripts/test-home.mjs
@@ -7,6 +7,9 @@ import { join } from "node:path";
const home = mkdtempSync(join(tmpdir(), "hf-test-home-"));
process.once("exit", () => rmSync(home, { recursive: true, force: true }));
process.env.HOME = process.env.USERPROFILE = home;
+// bun keeps its transpiler cache under HOME, so each run would rewrite it cold from every spawned CLI; on a
+// congested Windows system disk that write burst held CLI spawns past their timeout. Transpile in memory.
+process.env.BUN_RUNTIME_TRANSPILER_CACHE_PATH = "0";
for (const name of [
"XDG_CACHE_HOME",
"XDG_CONFIG_HOME",
diff --git a/packages/cli/src/testHome.test.ts b/packages/cli/src/testHome.test.ts
index 67294a8499..01a0a96a4e 100644
--- a/packages/cli/src/testHome.test.ts
+++ b/packages/cli/src/testHome.test.ts
@@ -1,6 +1,8 @@
-import { existsSync, realpathSync, rmSync } from "node:fs";
+import { spawnSync } from "node:child_process";
+import { existsSync, mkdtempSync, readdirSync, realpathSync, rmSync } from "node:fs";
import { homedir, tmpdir, userInfo } from "node:os";
-import { join, sep } from "node:path";
+import { join, resolve, sep } from "node:path";
+import { fileURLToPath } from "node:url";
import { afterEach, describe, expect, it, vi } from "vitest";
import { fetchRegistryManifest } from "./registry/remote.js";
@@ -38,4 +40,24 @@ describe("the CLI test run", () => {
expect(existsSync(cacheFile(homedir()))).toBe(true);
expect(leaked).toBe(false);
});
+
+ // A cold cache write from each spawned CLI held Windows runs past the spawn timeout (scripts/test-home.mjs).
+ it("spawns CLIs that write no transpiler cache into their home", () => {
+ const home = mkdtempSync(join(tmpdir(), "hf-transpiler-cache-"));
+ try {
+ const cli = resolve(fileURLToPath(import.meta.url), "..", "cli.ts");
+ const res = spawnSync("bun", ["run", cli, "--version"], {
+ encoding: "utf8",
+ timeout: 30_000,
+ env: { ...process.env, HOME: home, USERPROFILE: home },
+ });
+ expect(res.status, res.stderr).toBe(0);
+ const cached = (readdirSync(home, { recursive: true }) as string[]).filter((path) =>
+ path.split(/[\\/]/).includes("@t@"),
+ );
+ expect(cached).toEqual([]);
+ } finally {
+ rmSync(home, { recursive: true, force: true });
+ }
+ });
});
diff --git a/packages/engine/src/utils/ffprobe.test.ts b/packages/engine/src/utils/ffprobe.test.ts
index 7ddddf38b1..33e0cc3797 100644
--- a/packages/engine/src/utils/ffprobe.test.ts
+++ b/packages/engine/src/utils/ffprobe.test.ts
@@ -1816,7 +1816,7 @@ describe("audio duration decode probe never fails the call and needs no profile
`/tmp/start-offset-${Object.keys(format).length}.ts`,
);
expect(meta.durationSeconds).toBe(600); // 601.3 + 0.1 - 1.4 is within the margin
- expect(readInterval(calls)).toBe("600.4%");
+ expect(readInterval(calls)).toBe("600.4%+2147483647");
});
it.each([
@@ -1868,6 +1868,34 @@ describe.skipIf(!HAS_FFPROBE)(
expect(lyingMeta.durationSeconds).toBeCloseTo(trueMeta.durationSeconds, 1);
});
+ // ffprobe reads an open interval's end from uninitialised heap; glibc's MALLOC_PERTURB_
+ // makes that garbage deterministic on Linux, as Windows' heap does at random.
+ it("decodes the tail in one pass when ffprobe's heap is dirty", async () => {
+ const real = await vi.importActual<typeof import("child_process")>("child_process");
+ const probes: string[][] = [];
+ vi.resetModules();
+ vi.doMock("child_process", () => ({
+ ...real,
+ spawn: (command: string, args: string[], options: object) => {
+ probes.push(args);
+ return real.spawn(command, args, options);
+ },
+ }));
+ const previous = process.env.MALLOC_PERTURB_;
+ process.env.MALLOC_PERTURB_ = "1";
+ try {
+ const { extractAudioMetadata } = await import("./ffprobe.js");
+ expect((await extractAudioMetadata(lyingPath)).durationSeconds).toBeCloseTo(4, 3);
+ // The container probe, then the tail decode; a third call is the full-scan fallback.
+ expect(probes).toHaveLength(2);
+ } finally {
+ if (previous === undefined) delete process.env.MALLOC_PERTURB_;
+ else process.env.MALLOC_PERTURB_ = previous;
+ vi.doUnmock("child_process");
+ vi.resetModules();
+ }
+ });
+
// 2 s of AAC in MPEG-TS starting at 1.4 s: last frame 3.448 + 1024/16000 - 1.4.
it("does not add an MPEG-TS start time to the duration", async () => {
const { extractAudioMetadata } = await import("./ffprobe.js");
diff --git a/packages/engine/src/utils/ffprobe.ts b/packages/engine/src/utils/ffprobe.ts
index 6151efc545..ad63d91fff 100644
--- a/packages/engine/src/utils/ffprobe.ts
+++ b/packages/engine/src/utils/ffprobe.ts
@@ -165,6 +165,7 @@ const AUDIO_DURATION_PROBE_MARGIN_SECONDS = 0.05;
// How far before the container's claimed end the decode probe starts reading,
// so it decodes only the tail instead of the whole stream.
const AUDIO_DURATION_PROBE_TAIL_SECONDS = 1;
+const FFPROBE_READ_INTERVAL_EXPLICIT_FAR_END = "+2147483647";
export interface VideoColorSpace {
/** Color transfer characteristics, e.g. "bt709", "smpte2084", "arib-std-b67" */
@@ -1068,7 +1069,8 @@ async function probeDecodedAudioDuration(
startSeconds,
startSeconds + containerDurationSeconds - AUDIO_DURATION_PROBE_TAIL_SECONDS,
);
- const lastFrame = (await probe(`${tailStart}%`)) ?? (await probe());
+ const lastFrame =
+ (await probe(`${tailStart}%${FFPROBE_READ_INTERVAL_EXPLICIT_FAR_END}`)) ?? (await probe());
if (!lastFrame) return null;
return lastFrame.timestamp + lastFrame.nbSamples / sampleRate - startSeconds;
}