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

fix: coalesce concurrent last-frame extractions per clip (#11782)

[._.]/ Adam Eivy3d09a272de14
diff --git a/server/services/videoGen/frameExtraction.js b/server/services/videoGen/frameExtraction.js
index 08166d7712..5b0c36c698 100644
--- a/server/services/videoGen/frameExtraction.js
+++ b/server/services/videoGen/frameExtraction.js
@@ -2,6 +2,7 @@
import { existsSync, statSync } from 'fs';
import { join } from 'path';
+import { randomUUID } from 'crypto';
import { tmpdir } from 'os';
import { spawn } from '../../lib/childProcess.js';
import { ensureDir, PATHS, UUID_RE, tryReadFile, copyFileGuarded, writeFileGuarded, unlinkGuarded, rmGuarded } from '../../lib/fileUtils.js';
@@ -82,7 +83,22 @@ async function decodeTailCandidates(ffmpeg, videoPath, candidateDir) {
// When no candidate carries any signal at all (a genuinely black or flat
// tail) or nothing decodes, this falls back to the original single-seek
// behavior and says so: a degraded anchor still beats failing the chain.
-export async function extractLastFrame(historyId) {
+// Coalesce concurrent extractions of one clip: the on-disk cache is only
+// written at the end, so overlapping runs would both scan and the second would
+// delete the first's candidates mid-flight, pinning a degraded anchor.
+const inFlight = new Map();
+
+export function extractLastFrame(historyId) {
+ const pending = inFlight.get(historyId);
+ if (pending) return pending;
+ const run = extractLastFrameUncoalesced(historyId).finally(() => {
+ if (inFlight.get(historyId) === run) inFlight.delete(historyId);
+ });
+ inFlight.set(historyId, run);
+ return run;
+}
+
+async function extractLastFrameUncoalesced(historyId) {
// Keep this service safe for callers outside the route layer too. Shared
// gallery uploads deliberately use `upload-<uuid8>` ids, while generated
// clips retain UUID ids.
@@ -183,7 +199,7 @@ export async function extractLastFrame(historyId) {
// ── Scored pick over the tail window ──────────────────────────────────────
// Everything here degrades to the single-seek fallback below rather than
// throwing: this runs mid-chain, and a scan failure must not lose a render.
- const candidateDir = join(tmpdir(), `anchorcand-${item.id}`);
+ const candidateDir = join(tmpdir(), `anchorcand-${item.id}-${randomUUID()}`);
const { paths: candidates, complete: scanComplete } = await decodeTailCandidates(ffmpeg, videoPath, candidateDir)
.catch((err) => {
console.log(`⚠️ Anchor candidate scan failed: ${err.message}`);
diff --git a/server/services/videoGen/frameExtraction.test.js b/server/services/videoGen/frameExtraction.test.js
new file mode 100644
index 0000000000..39de1c0d91
--- /dev/null
+++ b/server/services/videoGen/frameExtraction.test.js
@@ -0,0 +1,63 @@
+import { describe, it, expect, vi, beforeEach, afterAll } from 'vitest';
+import { mkdtempSync, writeFileSync, rmSync, mkdirSync } from 'fs';
+import { join } from 'path';
+import { tmpdir } from 'os';
+
+const ID = '11111111-1111-4111-8111-111111111111';
+const root = mkdtempSync(join(tmpdir(), 'frameext-test-'));
+const videos = join(root, 'videos');
+const images = join(root, 'images');
+mkdirSync(videos, { recursive: true });
+writeFileSync(join(videos, 'clip.mp4'), 'x');
+
+const scanDirs = [];
+let release;
+
+vi.mock('../../lib/fileUtils.js', async (importOriginal) => {
+ const actual = await importOriginal();
+ return { ...actual, PATHS: { ...actual.PATHS, videos, images } };
+});
+vi.mock('./history.js', () => ({ loadHistory: async () => [{ id: ID, filename: 'clip.mp4' }] }));
+vi.mock('../../lib/ffmpeg.js', async (importOriginal) => ({
+ ...(await importOriginal()),
+ findFfmpeg: async () => '/bin/ffmpeg',
+ vfrSyncArgs: async () => [],
+ runFfmpegProcess: vi.fn(async ({ args }) => {
+ const out = args[args.length - 1];
+ scanDirs.push(out.replace(/\/cand-%03d\.png$/, ''));
+ await new Promise((r) => { release = r; });
+ writeFileSync(out.replace('%03d', '001'), 'frame');
+ return { ok: true };
+ }),
+}));
+vi.mock('../../lib/frameQuality.js', async (importOriginal) => ({
+ ...(await importOriginal()),
+ pickBestFrame: async (paths) => ({ path: paths[0], index: 0, focus: 1 }),
+}));
+
+const { extractLastFrame } = await import('./frameExtraction.js');
+const { runFfmpegProcess } = await import('../../lib/ffmpeg.js');
+
+describe('extractLastFrame concurrency', () => {
+ beforeEach(() => {
+ scanDirs.length = 0;
+ runFfmpegProcess.mockClear();
+ rmSync(images, { recursive: true, force: true });
+ });
+ afterAll(() => rmSync(root, { recursive: true, force: true }));
+
+ it('coalesces concurrent calls into one scan with a per-run candidate dir, then hits cache', async () => {
+ const a = extractLastFrame(ID);
+ const b = extractLastFrame(ID);
+ await vi.waitFor(() => expect(release).toBeTypeOf('function'));
+ release();
+ const [ra, rb] = await Promise.all([a, b]);
+ expect(runFfmpegProcess).toHaveBeenCalledTimes(1);
+ expect(rb).toEqual(ra);
+ expect(ra.filename).toBe(`anchor-${ID}.png`);
+ expect(scanDirs[0]).toMatch(new RegExp(`anchorcand-${ID}-[0-9a-f-]{36}$`));
+
+ await extractLastFrame(ID);
+ expect(runFfmpegProcess).toHaveBeenCalledTimes(1);
+ });
+});