Skip to content

Commit 3d6de05

Browse files
fix(engine): audio duration probe reads to the end on Windows; cli tests skip bun's disk cache (#5395)
1 parent 4b68f73 commit 3d6de05

4 files changed

Lines changed: 59 additions & 4 deletions

File tree

‎packages/cli/scripts/test-home.mjs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { join } from "node:path";
77
const home = mkdtempSync(join(tmpdir(), "hf-test-home-"));
88
process.once("exit", () => rmSync(home, { recursive: true, force: true }));
99
process.env.HOME = process.env.USERPROFILE = home;
10+
// bun keeps its transpiler cache under HOME, so each run would rewrite it cold from every spawned CLI; on a
11+
// congested Windows system disk that write burst held CLI spawns past their timeout. Transpile in memory.
12+
process.env.BUN_RUNTIME_TRANSPILER_CACHE_PATH = "0";
1013
for (const name of [
1114
"XDG_CACHE_HOME",
1215
"XDG_CONFIG_HOME",

‎packages/cli/src/testHome.test.ts‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { existsSync, realpathSync, rmSync } from "node:fs";
1+
import { spawnSync } from "node:child_process";
2+
import { existsSync, mkdtempSync, readdirSync, realpathSync, rmSync } from "node:fs";
23
import { homedir, tmpdir, userInfo } from "node:os";
3-
import { join, sep } from "node:path";
4+
import { join, resolve, sep } from "node:path";
5+
import { fileURLToPath } from "node:url";
46
import { afterEach, describe, expect, it, vi } from "vitest";
57
import { fetchRegistryManifest } from "./registry/remote.js";
68

@@ -38,4 +40,24 @@ describe("the CLI test run", () => {
3840
expect(existsSync(cacheFile(homedir()))).toBe(true);
3941
expect(leaked).toBe(false);
4042
});
43+
44+
// A cold cache write from each spawned CLI held Windows runs past the spawn timeout (scripts/test-home.mjs).
45+
it("spawns CLIs that write no transpiler cache into their home", () => {
46+
const home = mkdtempSync(join(tmpdir(), "hf-transpiler-cache-"));
47+
try {
48+
const cli = resolve(fileURLToPath(import.meta.url), "..", "cli.ts");
49+
const res = spawnSync("bun", ["run", cli, "--version"], {
50+
encoding: "utf8",
51+
timeout: 30_000,
52+
env: { ...process.env, HOME: home, USERPROFILE: home },
53+
});
54+
expect(res.status, res.stderr).toBe(0);
55+
const cached = (readdirSync(home, { recursive: true }) as string[]).filter((path) =>
56+
path.split(/[\\/]/).includes("@t@"),
57+
);
58+
expect(cached).toEqual([]);
59+
} finally {
60+
rmSync(home, { recursive: true, force: true });
61+
}
62+
});
4163
});

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1816,7 +1816,7 @@ describe("audio duration decode probe never fails the call and needs no profile
18161816
`/tmp/start-offset-${Object.keys(format).length}.ts`,
18171817
);
18181818
expect(meta.durationSeconds).toBe(600); // 601.3 + 0.1 - 1.4 is within the margin
1819-
expect(readInterval(calls)).toBe("600.4%");
1819+
expect(readInterval(calls)).toBe("600.4%+2147483647");
18201820
});
18211821

18221822
it.each([
@@ -1868,6 +1868,34 @@ describe.skipIf(!HAS_FFPROBE)(
18681868
expect(lyingMeta.durationSeconds).toBeCloseTo(trueMeta.durationSeconds, 1);
18691869
});
18701870

1871+
// ffprobe reads an open interval's end from uninitialised heap; glibc's MALLOC_PERTURB_
1872+
// makes that garbage deterministic on Linux, as Windows' heap does at random.
1873+
it("decodes the tail in one pass when ffprobe's heap is dirty", async () => {
1874+
const real = await vi.importActual<typeof import("child_process")>("child_process");
1875+
const probes: string[][] = [];
1876+
vi.resetModules();
1877+
vi.doMock("child_process", () => ({
1878+
...real,
1879+
spawn: (command: string, args: string[], options: object) => {
1880+
probes.push(args);
1881+
return real.spawn(command, args, options);
1882+
},
1883+
}));
1884+
const previous = process.env.MALLOC_PERTURB_;
1885+
process.env.MALLOC_PERTURB_ = "1";
1886+
try {
1887+
const { extractAudioMetadata } = await import("./ffprobe.js");
1888+
expect((await extractAudioMetadata(lyingPath)).durationSeconds).toBeCloseTo(4, 3);
1889+
// The container probe, then the tail decode; a third call is the full-scan fallback.
1890+
expect(probes).toHaveLength(2);
1891+
} finally {
1892+
if (previous === undefined) delete process.env.MALLOC_PERTURB_;
1893+
else process.env.MALLOC_PERTURB_ = previous;
1894+
vi.doUnmock("child_process");
1895+
vi.resetModules();
1896+
}
1897+
});
1898+
18711899
// 2 s of AAC in MPEG-TS starting at 1.4 s: last frame 3.448 + 1024/16000 - 1.4.
18721900
it("does not add an MPEG-TS start time to the duration", async () => {
18731901
const { extractAudioMetadata } = await import("./ffprobe.js");

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ const AUDIO_DURATION_PROBE_MARGIN_SECONDS = 0.05;
165165
// How far before the container's claimed end the decode probe starts reading,
166166
// so it decodes only the tail instead of the whole stream.
167167
const AUDIO_DURATION_PROBE_TAIL_SECONDS = 1;
168+
const FFPROBE_READ_INTERVAL_EXPLICIT_FAR_END = "+2147483647";
168169

169170
export interface VideoColorSpace {
170171
/** Color transfer characteristics, e.g. "bt709", "smpte2084", "arib-std-b67" */
@@ -1068,7 +1069,8 @@ async function probeDecodedAudioDuration(
10681069
startSeconds,
10691070
startSeconds + containerDurationSeconds - AUDIO_DURATION_PROBE_TAIL_SECONDS,
10701071
);
1071-
const lastFrame = (await probe(`${tailStart}%`)) ?? (await probe());
1072+
const lastFrame =
1073+
(await probe(`${tailStart}%${FFPROBE_READ_INTERVAL_EXPLICIT_FAR_END}`)) ?? (await probe());
10721074
if (!lastFrame) return null;
10731075
return lastFrame.timestamp + lastFrame.nbSamples / sampleRate - startSeconds;
10741076
}

0 commit comments

Comments
 (0)