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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
feat([issue-724]): add panel-side SIGKILL watchdog for hung video children
[._.]/ Adam Eivy3c038e556f83
diff --git a/server/services/videoGen/local.js b/server/services/videoGen/local.jsindex c3c6fae013..66656174d4 100644--- a/server/services/videoGen/local.js+++ b/server/services/videoGen/local.js@@ -56,6 +56,28 @@ const IS_WIN = process.platform === 'win32';const MODULE_NOT_FOUND_RE = /ModuleNotFoundError: No module named ['"]([^'"]+)['"]/;+// Panel-side SIGKILL watchdog (defense-in-depth at the Node layer).+//+// Some video runtimes finish all real work — decode, mux, write the file, and+// print the final `{"video_path": ...}` JSON — yet the python process lingers+// instead of exiting (a known mlx/torch teardown hang where a daemon thread or+// GPU context keeps the interpreter alive). The helper-side `os._exit(0)` is the+// first line of defense; this watchdog is the second. Once we observe the+// render's completion marker on stdout (the muxing-done line or the result+// JSON), we arm a grace timer; if the child still hasn't emitted 'close' by the+// time it fires, we SIGKILL it so the job (and the serialized gpu lane) doesn't+// wedge forever. The timer is cleared in every exit path so it can never fire+// against a recycled PID.+const COMPLETION_WATCHDOG_GRACE_MS = (() => {+ const raw = parseInt(process.env.VIDEOGEN_COMPLETION_WATCHDOG_MS || '', 10);+ return Number.isFinite(raw) && raw > 0 ? raw : 40000;+})();++// Upstream prints this when the final decode+mux finishes, just before it should+// exit. Matching it (case-insensitive) lets us arm the watchdog even for+// runtimes that don't emit the result JSON on stdout.+const MUXING_DONE_RE = /\[Decoding video \+ audio \+ muxing\]\s+done in/i;+// Catalog comes from data/media-models.json (see server/lib/mediaModels.js).// Cached as a plain object at boot for O(1) lookup by id, matching the prior shape.export const VIDEO_MODELS = Object.fromEntries(getVideoModels().map((m) => [m.id, m]));@@ -741,6 +763,37 @@ export async function generateVideo({ pythonPath, prompt, negativePrompt = '', mchildEnv.PYTHONUNBUFFERED = '1';const proc = spawn(bin, args, { env: childEnv, stdio: ['ignore', 'pipe', 'pipe'] });activeProcess = proc;++ // Panel-side completion watchdog. Armed once we see the render's completion+ // marker on stdout; SIGKILLs the child if it hasn't exited after the grace+ // window. clearCompletionWatchdog() runs in every terminal path ('close',+ // 'error') so the timer can't outlive this child or fire against a recycled+ // PID. Armed at most once per child (re-seeing the marker is a no-op).+ let completionWatchdog = null;+ const clearCompletionWatchdog = () => {+ if (completionWatchdog) {+ clearTimeout(completionWatchdog);+ completionWatchdog = null;+ }+ };+ const armCompletionWatchdog = () => {+ if (completionWatchdog) return;+ completionWatchdog = setTimeout(() => {+ // Runs outside the Express request lifecycle — an uncaught throw here+ // would crash the Node process, so guard the whole body.+ try {+ completionWatchdog = null;+ if (activeProcess !== proc || proc.exitCode !== null || proc.signalCode !== null) return;+ console.log(`⚠️ video child reported completion but never exited — SIGKILL [${jobId.slice(0, 8)}]`);+ proc.kill('SIGKILL');+ } catch (err) {+ console.error(`❌ completion watchdog failed [${jobId.slice(0, 8)}]: ${err.message}`);+ }+ }, COMPLETION_WATCHDOG_GRACE_MS);+ // Don't let the watchdog timer keep the event loop alive on its own.+ if (typeof completionWatchdog.unref === 'function') completionWatchdog.unref();+ };+// Hold a sleep-prevention lock for the lifetime of the python child, so a// 90s+ render doesn't get aborted by sleep on a laptop. `-s` blocks system// sleep (lid-close / low-power), `-i` blocks idle sleep, `-d` blocks display@@ -754,6 +807,7 @@ export async function generateVideo({ pythonPath, prompt, negativePrompt = '', m// Without an 'error' handler, a missing/non-executable pythonPath would// crash the server with an unhandled error event.proc.on('error', (err) => {+ clearCompletionWatchdog();job.status = 'error';const reason = `Failed to spawn ${bin}: ${err.message}`;console.log(`❌ Video generation spawn error [${jobId.slice(0, 8)}]: ${reason}`);@@ -871,9 +925,17 @@ export async function generateVideo({ pythonPath, prompt, negativePrompt = '', m// for the result metadata; otherwise raw-log so we can debug failures.try {const parsed = JSON.parse(line);- if (parsed.video_path) job.resultJson = parsed;+ if (parsed.video_path) {+ job.resultJson = parsed;+ // The result JSON is the strongest "work is done" signal — arm the+ // watchdog so a post-completion teardown hang can't wedge the job.+ armCompletionWatchdog();+ }continue;} catch { /* not JSON */ }+ // Some runtimes don't print the result JSON but do log the final+ // decode+mux line right before they should exit — treat it the same way.+ if (MUXING_DONE_RE.test(line)) armCompletionWatchdog();console.log(`🐍-out [${jobId.slice(0, 8)}] ${line}`);}});@@ -891,6 +953,7 @@ export async function generateVideo({ pythonPath, prompt, negativePrompt = '', m});proc.on('close', async (code, signal) => {+ clearCompletionWatchdog();activeProcess = null;// Cleanup the resized temp images if we made them. Track via flags rather// than a path-prefix check — tmpdir() can return a symlinked pathdiff --git a/server/services/videoGen/local.test.js b/server/services/videoGen/local.test.jsindex 9c08f850d0..12f69e8748 100644--- a/server/services/videoGen/local.test.js+++ b/server/services/videoGen/local.test.js@@ -457,3 +457,157 @@ describe('FFLF/ltx2 pixel-budget helpers', () => {});});});++describe('generateVideo — panel-side completion watchdog', () => {+ // Build a fake child that does NOT auto-exit, exposing handles to its stdout+ // 'data' listener and 'close' handler so the test can drive completion+ // detection + the grace-timer escalation deterministically.+ function makeHangingProc() {+ const listeners = {};+ let stdoutData = null;+ const proc = {+ pid: 4242,+ exitCode: null, // stays null — the child never exits on its own+ signalCode: null,+ killed: false,+ stdout: { on: vi.fn((event, fn) => { if (event === 'data') stdoutData = fn; }) },+ stderr: { on: vi.fn() },+ on(event, fn) { listeners[event] = fn; return proc; },+ kill: vi.fn((signal) => { proc.killed = true; proc.signalCode = signal; }),+ };+ return {+ proc,+ emitStdout: (text) => stdoutData?.(Buffer.from(text)),+ fireClose: (code, signal) => listeners.close?.(code, signal),+ };+ }++ let restoreSpawn;+ beforeEach(async () => {+ const { spawn } = await import('child_process');+ restoreSpawn = vi.mocked(spawn).getMockImplementation();+ vi.useFakeTimers();+ });++ afterEach(() => {+ vi.useRealTimers();+ delete process.env.VIDEOGEN_COMPLETION_WATCHDOG_MS;+ });++ it('SIGKILLs a child that prints the result JSON but never exits, after the grace window', async () => {+ process.env.VIDEOGEN_COMPLETION_WATCHDOG_MS = '40000';+ // re-import so the module-level grace constant picks up the env override+ vi.resetModules();+ ({ generateVideo } = await import('./local.js'));++ const { spawn } = await import('child_process');+ const hang = makeHangingProc();+ vi.mocked(spawn).mockImplementationOnce(() => hang.proc);++ generateVideo({+ jobId: 'watchdog-json-hang',+ pythonPath: '/usr/bin/python3',+ modelId: 'ltx2_unified',+ prompt: 'render and hang',+ width: 512, height: 512, numFrames: 25, fps: 24,+ mode: 'text',+ });++ // Let generateVideo run far enough to register stdout/close handlers.+ await vi.advanceTimersByTimeAsync(0);++ // The render finishes its real work and emits the result JSON, then hangs.+ hang.emitStdout('{"video_path": "/data/videos/out.mp4"}\n');+ expect(hang.proc.kill).not.toHaveBeenCalled();++ // Just before the grace window, still no kill.+ await vi.advanceTimersByTimeAsync(39999);+ expect(hang.proc.kill).not.toHaveBeenCalled();++ // Past the grace window — the watchdog escalates to SIGKILL.+ await vi.advanceTimersByTimeAsync(2);+ expect(hang.proc.kill).toHaveBeenCalledWith('SIGKILL');+ });++ it('SIGKILLs a child that prints the muxing-done line but never exits', async () => {+ process.env.VIDEOGEN_COMPLETION_WATCHDOG_MS = '40000';+ vi.resetModules();+ ({ generateVideo } = await import('./local.js'));++ const { spawn } = await import('child_process');+ const hang = makeHangingProc();+ vi.mocked(spawn).mockImplementationOnce(() => hang.proc);++ generateVideo({+ jobId: 'watchdog-mux-hang',+ pythonPath: '/usr/bin/python3',+ modelId: 'ltx2_unified',+ prompt: 'mux and hang',+ width: 512, height: 512, numFrames: 25, fps: 24,+ mode: 'text',+ });+ await vi.advanceTimersByTimeAsync(0);++ hang.emitStdout('[Decoding video + audio + muxing] done in 3.2s\n');+ await vi.advanceTimersByTimeAsync(40001);+ expect(hang.proc.kill).toHaveBeenCalledWith('SIGKILL');+ });++ it('does NOT SIGKILL when the child exits cleanly after completion (timer is cleared on close)', async () => {+ process.env.VIDEOGEN_COMPLETION_WATCHDOG_MS = '40000';+ vi.resetModules();+ ({ generateVideo } = await import('./local.js'));++ const { spawn } = await import('child_process');+ const hang = makeHangingProc();+ vi.mocked(spawn).mockImplementationOnce(() => hang.proc);++ generateVideo({+ jobId: 'watchdog-clean-exit',+ pythonPath: '/usr/bin/python3',+ modelId: 'ltx2_unified',+ prompt: 'render and exit',+ width: 512, height: 512, numFrames: 25, fps: 24,+ mode: 'text',+ });+ await vi.advanceTimersByTimeAsync(0);++ // Completion marker arms the watchdog…+ hang.emitStdout('{"video_path": "/data/videos/out.mp4"}\n');+ // …but the child then exits cleanly well within the grace window.+ hang.proc.exitCode = 0;+ hang.fireClose(0, null);+ await vi.advanceTimersByTimeAsync(0);++ // Advancing past the grace window must NOT trigger a SIGKILL — the close+ // handler cleared the timer.+ await vi.advanceTimersByTimeAsync(60000);+ expect(hang.proc.kill).not.toHaveBeenCalled();+ });++ it('never arms the watchdog when no completion marker is seen', async () => {+ process.env.VIDEOGEN_COMPLETION_WATCHDOG_MS = '40000';+ vi.resetModules();+ ({ generateVideo } = await import('./local.js'));++ const { spawn } = await import('child_process');+ const hang = makeHangingProc();+ vi.mocked(spawn).mockImplementationOnce(() => hang.proc);++ generateVideo({+ jobId: 'watchdog-no-marker',+ pythonPath: '/usr/bin/python3',+ modelId: 'ltx2_unified',+ prompt: 'progress only',+ width: 512, height: 512, numFrames: 25, fps: 24,+ mode: 'text',+ });+ await vi.advanceTimersByTimeAsync(0);++ // Ordinary progress lines must not arm the watchdog.+ hang.emitStdout('STAGE:render:step:5:30:rendering\n');+ hang.emitStdout('60%|██████ | 6/10\n');+ await vi.advanceTimersByTimeAsync(120000);+ expect(hang.proc.kill).not.toHaveBeenCalled();+ });+});