Skip to content

Commit f13037e

Browse files
authored
fix(engine): isolate chunked encode temporary files (#3680)
1 parent ae3d80c commit f13037e

2 files changed

Lines changed: 59 additions & 5 deletions

File tree

‎packages/engine/src/services/chunkEncoder.test.ts‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { EventEmitter } from "node:events";
2-
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2+
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
33
import { tmpdir } from "node:os";
4-
import { join } from "node:path";
4+
import { dirname, join } from "node:path";
55
import { afterEach, describe, it, expect, vi } from "vitest";
66
import { ENCODER_PRESETS, getEncoderPreset, buildEncoderArgs } from "./chunkEncoder.js";
77
import { renderProvenanceArgs } from "../utils/renderProvenance.js";
@@ -229,6 +229,50 @@ describe("encodeFramesFromDir ffmpegEncodeTimeout", () => {
229229
});
230230

231231
describe("encodeFramesChunkedConcat ffmpegEncodeTimeout", () => {
232+
it("isolates concurrent encodes and preserves pre-existing chunk files", async () => {
233+
const { spawn, calls } = createSpawnSpy();
234+
vi.resetModules();
235+
vi.doMock("child_process", () => ({ spawn }));
236+
const { encodeFramesChunkedConcat } = await import("./chunkEncoder.js");
237+
const { root, framesDir } = createFrameFixture();
238+
const legacyDir = join(root, "chunk-encode");
239+
mkdirSync(legacyDir);
240+
const legacyList = join(legacyDir, "concat-list.txt");
241+
writeFileSync(legacyList, "another render's concat list");
242+
243+
const results = ["first.mp4", "second.mp4"].map((name) =>
244+
encodeFramesChunkedConcat(
245+
framesDir,
246+
"frame_%06d.png",
247+
join(root, name),
248+
tinyEncodeOptions,
249+
30,
250+
),
251+
);
252+
expect(calls).toHaveLength(2);
253+
const chunkPaths = calls.map((call) => call.args.at(-1));
254+
for (const call of [...calls]) emitClose(call.proc, 0);
255+
await flushManagedProcessResolution();
256+
expect(calls).toHaveLength(4);
257+
const lists = calls.slice(2).map((call) => call.args[call.args.indexOf("-i") + 1]);
258+
for (const call of calls.slice(2)) emitClose(call.proc, 0);
259+
const completed = await Promise.all(results);
260+
261+
expect(completed.every((result) => result.success && result.framesEncoded === 2)).toBe(true);
262+
expect(new Set(chunkPaths).size).toBe(2);
263+
expect(new Set(lists).size).toBe(2);
264+
for (let index = 0; index < 2; index++) {
265+
const chunkPath = chunkPaths[index];
266+
const list = lists[index];
267+
if (!chunkPath || !list) throw new Error("Expected chunk and concat paths");
268+
expect(dirname(chunkPath)).not.toBe(legacyDir);
269+
expect(dirname(list)).toBe(dirname(chunkPath));
270+
expect(dirname(dirname(list))).toBe(root);
271+
expect(readFileSync(list, "utf8")).toBe(`file '${chunkPath.replace(/'/g, "'\\''")}'`);
272+
}
273+
expect(readFileSync(legacyList, "utf8")).toBe("another render's concat list");
274+
});
275+
232276
it("passes config timeout to per-chunk encodes", async () => {
233277
vi.useFakeTimers();
234278
const { spawn, calls } = createSpawnSpy();

‎packages/engine/src/services/chunkEncoder.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66
* Supports CPU (libx264) and GPU encoding.
77
*/
88

9-
import { copyFileSync, existsSync, mkdirSync, readdirSync, statSync, writeFileSync } from "fs";
9+
import {
10+
copyFileSync,
11+
existsSync,
12+
mkdirSync,
13+
mkdtempSync,
14+
readdirSync,
15+
statSync,
16+
writeFileSync,
17+
} from "fs";
1018
import { join, dirname, extname } from "path";
1119
import { DEFAULT_CONFIG, type EngineConfig } from "../config.js";
1220
import {
@@ -556,8 +564,10 @@ export async function encodeFramesChunkedConcat(
556564
}
557565
const chunkSize = Math.max(30, Math.floor(chunkSizeFrames));
558566
const chunkCount = Math.ceil(files.length / chunkSize);
559-
const chunkDir = join(dirname(outputPath), "chunk-encode");
560-
if (!existsSync(chunkDir)) mkdirSync(chunkDir, { recursive: true });
567+
mkdirSync(dirname(outputPath), { recursive: true });
568+
// Keep intermediates under the caller-owned output directory for its existing
569+
// cleanup/debug policy, but never reuse another invocation's chunk files.
570+
const chunkDir = mkdtempSync(join(dirname(outputPath), "chunk-encode-"));
561571
const chunkPaths: string[] = [];
562572

563573
for (let i = 0; i < chunkCount; i++) {

0 commit comments

Comments
 (0)