Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (claude): clean up resize temp files when Ingredients still-prep fails

[._.]/ Adam Eivy2199013bdfc0
diff --git a/server/services/videoGen/local.js b/server/services/videoGen/local.js
index 5de503253f..0e05305a21 100644
--- a/server/services/videoGen/local.js
+++ b/server/services/videoGen/local.js
@@ -910,10 +910,25 @@ export async function generateVideo({ pythonPath, prompt, negativePrompt = '', m
// temp paths are tracked for the same cleanup sites.
const icReferenceTempPaths = [];
let resolvedIcReferencePaths = icReferencePaths;
+ // Both throw sites in this block land BEFORE the buildArgs try/catch below
+ // (whose catch is what normally unlinks resizedSrcTempPath/
+ // resizedLastTempPath/resizedKeyframeTempPaths/icReferenceTempPaths) — a
+ // throw here escapes uncaught by that cleanup, and the caller's outer catch
+ // only unlinks the route-level upload/audio temp files, not these
+ // internally-created resize/still-clip temp files. Clean them up explicitly
+ // before either throw so a missing ffmpeg or a failed still-encode doesn't
+ // leak every resized temp file for the request into os.tmpdir().
+ const cleanupResizeTempFiles = async () => {
+ if (resizedSrcTempPath) await unlink(resizedSrcTempPath).catch(() => {});
+ if (resizedLastTempPath) await unlink(resizedLastTempPath).catch(() => {});
+ for (const p of resizedKeyframeTempPaths) await unlink(p).catch(() => {});
+ for (const p of icReferenceTempPaths) await unlink(p).catch(() => {});
+ };
if (isIcLoraMode(mode) && icLoraSpecForMode(mode)?.referenceKind === 'image'
&& Array.isArray(icReferencePaths) && icReferencePaths.length) {
const stillFfmpeg = ffmpeg || await findFfmpeg();
if (!stillFfmpeg) {
+ await cleanupResizeTempFiles();
throw new ServerError(
'ffmpeg is required to prepare still references for Ingredients mode — install it (brew install ffmpeg) and retry.',
{ status: 400, code: 'IC_LORA_STILL_NEEDS_FFMPEG' },
@@ -940,7 +955,9 @@ export async function generateVideo({ pythonPath, prompt, negativePrompt = '', m
// Unlike the resizeImage fallback (which degrades to the original), there is
// no usable degradation here — a still handed straight to the pipeline fails
// deep inside the VAE reshape. Fail loudly with the ffmpeg reason.
- for (const p of clipPaths) await unlink(p).catch(() => {});
+ // cleanupResizeTempFiles() also unlinks clipPaths (already pushed into
+ // icReferenceTempPaths above), plus any earlier resize temp files.
+ await cleanupResizeTempFiles();
throw new ServerError(
`Failed to prepare Ingredients reference ${basename(icReferencePaths[failedAt])}: ${encodes[failedAt].error.message}`,
{ status: 400, code: 'IC_LORA_STILL_PREP_FAILED' },
diff --git a/server/services/videoGen/local.test.js b/server/services/videoGen/local.test.js
index 2d36f11a64..7a2fc554ed 100644
--- a/server/services/videoGen/local.test.js
+++ b/server/services/videoGen/local.test.js
@@ -2061,6 +2061,38 @@ describe('generateVideo — IC-LoRA remix arg threading (#3100)', () => {
execFileMock.mockImplementation((_bin, _args, _opts, cb) => cb?.(null, '', ''));
});
+ it('also cleans up an earlier resized-source temp file when a still fails to encode', async () => {
+ // resizedSrcTempPath is created by an EARLIER, unrelated resize step
+ // (mirrors a caller that also passed sourceImagePath) — before the
+ // buildArgs try/catch that normally cleans it up. A still-encode
+ // failure in the Ingredients block throws before ever reaching that
+ // try/catch, so without explicit cleanup here the resized-source temp
+ // file would leak into os.tmpdir() even though the clip temp files
+ // themselves were already covered by the test above.
+ const { execFile } = await import('child_process');
+ const { unlink } = await import('fs/promises');
+ const execFileMock = vi.mocked(execFile);
+ const unlinkMock = vi.mocked(unlink);
+ execFileMock.mockClear();
+ unlinkMock.mockClear();
+
+ // The plain resize call (no `-loop`) succeeds; every still-clip call
+ // (`-loop` present) fails.
+ execFileMock.mockImplementation((_bin, args, _opts, cb) => {
+ if (args.includes('-loop')) return cb?.(new Error('ffmpeg exploded'));
+ return cb?.(null, '', '');
+ });
+
+ await expect(generateVideo({
+ ...baseIngredients, jobId: 'ing-src-leak', sourceImagePath: '/mock/images/source.png',
+ })).rejects.toThrow(/Failed to prepare Ingredients reference/);
+
+ const unlinked = unlinkMock.mock.calls.map(([p]) => String(p));
+ expect(unlinked.some((p) => p.includes('resized-src-ing-src-leak.png'))).toBe(true);
+
+ execFileMock.mockImplementation((_bin, _args, _opts, cb) => cb?.(null, '', ''));
+ });
+
it('cleans up the temp clips after a successful render', async () => {
const { unlink } = await import('fs/promises');
const unlinkMock = vi.mocked(unlink);