Skip to content

Commit 3314168

Browse files
fix(producer): guard disk capture capacity (#3643)
1 parent c0303fc commit 3314168

2 files changed

Lines changed: 67 additions & 3 deletions

File tree

‎packages/producer/src/services/render/stages/captureStage.test.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { describe, expect, it } from "vitest";
2-
import { shouldAllowAdaptiveCaptureRetry } from "./captureStage.js";
2+
import {
3+
assertDiskCaptureHeadroom,
4+
estimateDiskCaptureBytes,
5+
shouldAllowAdaptiveCaptureRetry,
6+
} from "./captureStage.js";
37

48
describe("shouldAllowAdaptiveCaptureRetry", () => {
59
it("keeps timeout recovery enabled when the initial worker count was explicit", () => {
@@ -10,3 +14,23 @@ describe("shouldAllowAdaptiveCaptureRetry", () => {
1014
expect(shouldAllowAdaptiveCaptureRetry(1, true)).toBe(false);
1115
});
1216
});
17+
18+
describe("disk capture capacity", () => {
19+
const captureOptions = {
20+
width: 100,
21+
height: 50,
22+
fps: { num: 30, den: 1 },
23+
deviceScaleFactor: 2,
24+
format: "jpeg" as const,
25+
};
26+
27+
it("estimates output-resolution frame storage conservatively", () => {
28+
expect(estimateDiskCaptureBytes(10, captureOptions)).toBe(800_000);
29+
});
30+
31+
it("fails before capture when estimated frames exceed available headroom", () => {
32+
expect(() =>
33+
assertDiskCaptureHeadroom("/render/captured-frames", 10, captureOptions, () => 800_000),
34+
).toThrow(/may need ~0\.8 MB.*0\.8 MB is free.*--low-memory-mode/s);
35+
});
36+
});

‎packages/producer/src/services/render/stages/captureStage.ts‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* the stages can import them without reaching back into the orchestrator.
3737
*/
3838

39+
import { statfsSync } from "node:fs";
3940
import {
4041
type BeforeCaptureHook,
4142
type CaptureOptions,
@@ -136,6 +137,42 @@ export function shouldAllowAdaptiveCaptureRetry(
136137
return workerCount > 1;
137138
}
138139

140+
export function estimateDiskCaptureBytes(
141+
totalFrames: number,
142+
captureOptions: CaptureOptions,
143+
): number {
144+
const scale = captureOptions.deviceScaleFactor ?? 1;
145+
const outputWidth = Math.ceil(captureOptions.width * scale);
146+
const outputHeight = Math.ceil(captureOptions.height * scale);
147+
return Math.ceil(totalFrames) * outputWidth * outputHeight * 4;
148+
}
149+
150+
export function assertDiskCaptureHeadroom(
151+
framesDir: string,
152+
totalFrames: number,
153+
captureOptions: CaptureOptions,
154+
freeDiskBytes: (path: string) => number | null = (path) => {
155+
try {
156+
const stat = statfsSync(path);
157+
return stat.bavail * stat.bsize;
158+
} catch {
159+
return null;
160+
}
161+
},
162+
): void {
163+
const freeBytes = freeDiskBytes(framesDir);
164+
if (freeBytes === null) return;
165+
const estimatedBytes = estimateDiskCaptureBytes(totalFrames, captureOptions);
166+
if (estimatedBytes <= freeBytes * 0.9) return;
167+
throw new Error(
168+
`Disk capture may need ~${(estimatedBytes / 1e6).toFixed(1)} MB of temporary frame storage, ` +
169+
`but only ${(freeBytes / 1e6).toFixed(1)} MB is free at ${framesDir}. ` +
170+
"Re-run with --low-memory-mode to stream frames, raise " +
171+
"PRODUCER_STREAMING_ENCODE_MAX_DURATION_SECONDS if streaming is supported, " +
172+
"or free up disk space.",
173+
);
174+
}
175+
139176
export async function runCaptureStage(input: CaptureStageInput): Promise<CaptureStageResult> {
140177
const {
141178
fileServer,
@@ -194,6 +231,9 @@ export async function runCaptureStage(input: CaptureStageInput): Promise<Capture
194231
}
195232
}
196233

234+
const captureOptions = buildCaptureOptions();
235+
assertDiskCaptureHeadroom(framesDir, totalFrames, captureOptions);
236+
197237
if (workerCount > 1) {
198238
// Parallel capture. When `frameRange` is set (distributed chunk), pass
199239
// `frameRangeStart` so workers land on absolute composition frame indices
@@ -206,7 +246,7 @@ export async function runCaptureStage(input: CaptureStageInput): Promise<Capture
206246
initialWorkerCount: workerCount,
207247
allowRetry: shouldAllowAdaptiveCaptureRetry(workerCount, job.config.workers !== undefined),
208248
frameExt: needsAlpha ? "png" : "jpg",
209-
captureOptions: buildCaptureOptions(),
249+
captureOptions,
210250
createBeforeCaptureHook: createRenderVideoFrameInjector,
211251
abortSignal,
212252
frameRangeStart: frameRange?.startFrame,
@@ -252,7 +292,7 @@ export async function runCaptureStage(input: CaptureStageInput): Promise<Capture
252292
(await createCaptureSession(
253293
fileServer.url,
254294
framesDir,
255-
buildCaptureOptions(),
295+
captureOptions,
256296
videoInjector,
257297
captureCfg,
258298
));

0 commit comments

Comments
 (0)