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: extend-pick race token, queue BUSY backoff, gallery resolver requires regular file

Adam Eivy73b5b5fff8b8
diff --git a/client/src/pages/VideoGen.jsx b/client/src/pages/VideoGen.jsx
index 7340229341..9fb46cf335 100644
--- a/client/src/pages/VideoGen.jsx
+++ b/client/src/pages/VideoGen.jsx
@@ -249,7 +249,15 @@ export default function VideoGen() {
// Extend mode: the user picks a prior video; we extract its last frame
// (lazily — only when picked, since extraction shells out to ffmpeg) and
// use that as the source image for image-to-video.
+ //
+ // The pick token guards against a slow-then-fast race: if the user picks
+ // video A, then quickly switches to video B, A's extract response could
+ // arrive after B's and overwrite sourceImageFile with the wrong frame.
+ // Capture the token at request time and only apply the result when it
+ // still matches the latest pick.
+ const extendPickTokenRef = useRef(0);
const handleExtendPick = async (videoId) => {
+ const token = ++extendPickTokenRef.current;
setExtendFromVideoId(videoId);
if (!videoId) { clearSourceImage(); return; }
setExtendingFrame(true);
@@ -257,6 +265,10 @@ export default function VideoGen() {
toast.error(err.message || 'Failed to extract last frame');
return null;
});
+ // Drop the result if a newer pick has happened in the meantime — the
+ // newer request's setExtendingFrame(false) and setSourceImageFile will
+ // be the source of truth.
+ if (token !== extendPickTokenRef.current) return;
setExtendingFrame(false);
if (res?.filename) {
setSourceImageFile(res.filename);
@@ -386,6 +398,12 @@ export default function VideoGen() {
// Queue worker — pumps the head of the queue when nothing's running.
// Runs as an effect so it picks up any newly-enqueued item even while
// the user is interacting with the form.
+ //
+ // BUSY backoff: the server's `cancel()` keeps `activeProcess` set until
+ // the SIGKILL'd child actually exits (up to ~8s), so a freshly-cancelled
+ // item leaving the running slot here will often hit a 409 VIDEO_GEN_BUSY
+ // when the worker tries to dispatch the next pending item. Treat that as
+ // "not yet" (return the item to pending) instead of marking it errored.
useEffect(() => {
if (generating || runningQueueId) return;
const next = queue.find((item) => item.status === 'pending');
@@ -394,12 +412,25 @@ export default function VideoGen() {
setQueue((q) => q.map((item) => item.id === next.id ? { ...item, status: 'running', startedAt: Date.now() } : item));
const payload = { ...next.params };
if (next._blob) payload.sourceImage = next._blob;
+ let busyRetry = false;
runGeneration(payload).then((res) => {
setQueue((q) => q.map((item) => item.id === next.id ? { ...item, status: 'complete', result: res } : item));
}).catch((err) => {
+ const isBusy = /already in progress|VIDEO_GEN_BUSY|409/i.test(err?.message || '');
+ if (isBusy) {
+ // Bounce the item back to pending after a short delay so the worker
+ // re-tries once the server's previous child has finished cleaning up.
+ busyRetry = true;
+ setQueue((q) => q.map((item) => item.id === next.id ? { ...item, status: 'pending', startedAt: undefined } : item));
+ setTimeout(() => setRunningQueueId((curr) => (curr === next.id ? null : curr)), 1500);
+ return;
+ }
setQueue((q) => q.map((item) => item.id === next.id ? { ...item, status: 'error', error: err.message } : item));
}).finally(() => {
- setRunningQueueId(null);
+ // For the BUSY branch the timeout above releases the slot — releasing
+ // it here too would let the worker immediately re-fire and hit the
+ // same 409 before the server's old child has exited.
+ if (!busyRetry) setRunningQueueId(null);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [queue, generating, runningQueueId]);
diff --git a/server/routes/videoGen.js b/server/routes/videoGen.js
index e13acb5e46..59ad05d888 100644
--- a/server/routes/videoGen.js
+++ b/server/routes/videoGen.js
@@ -7,7 +7,7 @@
*/
import { Router } from 'express';
-import { existsSync } from 'fs';
+import { existsSync, statSync } from 'fs';
import { unlink } from 'fs/promises';
import { join, basename, resolve as resolvePath, sep as PATH_SEP } from 'path';
import { z } from 'zod';
@@ -88,11 +88,19 @@ router.get('/models', (_req, res) => {
});
// Path-traversal guard: basename() strips dirs, then resolve+prefix-check
-// against PATHS.images so a unicode trick can't escape data/images.
+// against PATHS.images so a unicode trick can't escape data/images. Also
+// reject `.`/`..`/empty basenames and require the resolved entry to be a
+// regular file — otherwise the images-root directory itself would resolve
+// (existsSync is true for dirs) and flow into ffmpeg as an "image path"
+// where it'd fail in confusing ways.
const resolveGalleryImage = (name) => {
+ const safe = basename(name);
+ if (!safe || safe === '.' || safe === '..') return null;
const imagesRoot = resolvePath(PATHS.images) + PATH_SEP;
- const localPath = resolvePath(join(PATHS.images, basename(name)));
- return localPath.startsWith(imagesRoot) && existsSync(localPath) ? localPath : null;
+ const localPath = resolvePath(join(PATHS.images, safe));
+ if (!localPath.startsWith(imagesRoot) || !existsSync(localPath)) return null;
+ const stat = statSync(localPath, { throwIfNoEntry: false });
+ return stat?.isFile() ? localPath : null;
};
router.post('/', sourceImageUpload, asyncHandler(async (req, res) => {
diff --git a/server/routes/videoGen.test.js b/server/routes/videoGen.test.js
index 93d83ebe78..ee044dbc3c 100644
--- a/server/routes/videoGen.test.js
+++ b/server/routes/videoGen.test.js
@@ -28,7 +28,13 @@ vi.mock('../lib/fileUtils.js', () => ({
PATHS: { images: '/mock/images' },
}));
-vi.mock('fs', () => ({ existsSync: vi.fn(() => true) }));
+vi.mock('fs', () => ({
+ existsSync: vi.fn(() => true),
+ // statSync gates resolveGalleryImage onto regular-file checks — the route
+ // rejects directories, so the mock has to look like a file for the
+ // gallery-image plumbing tests to pass.
+ statSync: vi.fn(() => ({ isFile: () => true })),
+}));
vi.mock('fs/promises', () => ({ unlink: vi.fn(async () => {}) }));
import * as videoGenService from '../services/videoGen/local.js';