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
address review (codex): refuse cancel during the async terminal-drain window
[._.]/ Adam Eivyd2672afb3720
diff --git a/server/services/mediaJobQueue/index.js b/server/services/mediaJobQueue/index.jsindex 88f5e3b7e1..1eb4f5d4c8 100644--- a/server/services/mediaJobQueue/index.js+++ b/server/services/mediaJobQueue/index.js@@ -548,7 +548,6 @@ async function runJob(job) {let progressPersistDirty = false;let progressPersistTimer = null;let progressPersisting = null;- let terminalStarted = false;const flushProgressPersist = async () => {if (progressPersistTimer) {@@ -582,10 +581,17 @@ async function runJob(job) {// canceled, watchdog) funnel through here. The status check at the top// ensures only the first caller wins; any subsequent call (e.g. watchdog// fired and then the gen emits 'completed') is a no-op.+ //+ // `job.terminating` (not a closure-local) is the guard so cancelJob — a+ // module-level function — can also see that a terminal transition has begun.+ // It's set synchronously before the async drain below, during which+ // job.status is still 'running'; a cancel arriving in that window must not+ // fire a redundant provider cancel. It's a transient in-memory flag+ // (excluded from persistImpl's serialized field set).let watchdogTimer;async function terminate(state, apply) {- if (terminalStarted || job.status !== 'running') return;- terminalStarted = true;+ if (job.terminating || job.status !== 'running') return;+ job.terminating = true;// setInterval now (was setTimeout) — using clearInterval to match the new// API. (Node accepts either clearTimeout or clearInterval on the same// Timeout handle, so this is purely stylistic.)@@ -617,7 +623,7 @@ async function runJob(job) {const handlers = {progress: (payload) => {- if (terminalStarted || job.status !== 'running') return;+ if (job.terminating || job.status !== 'running') return;let didUpdatePersistedProgress = false;if (payload.type === 'progress' && typeof payload.progress === 'number' && Number.isFinite(payload.progress)) {job.progress = Math.max(0, Math.min(1, payload.progress));@@ -714,10 +720,10 @@ async function runJob(job) {let watchdogInFlight = false;watchdogTimer = setInterval(async () => {if (watchdogInFlight) return;- // terminalStarted closes the window terminate() opens: it now awaits a+ // job.terminating closes the window terminate() opens: it now awaits a// disk drain before flipping job.status, so a tick that only checked// status could fire mod.cancel on a job that is already terminating.- if (terminalStarted || job.status !== 'running') return;+ if (job.terminating || job.status !== 'running') return;const idleFor = Date.now() - lastActivityAt;if (idleFor < idleTimeoutMs) return;watchdogInFlight = true;@@ -732,7 +738,7 @@ async function runJob(job) {const mod = await getGenModuleForJob(job);// Re-check after the await — terminate() may have started (and is// draining to disk with job.status still 'running') during the import.- if (terminalStarted || job.status !== 'running') return;+ if (job.terminating || job.status !== 'running') return;console.log(`⏱️ media-job [${job.id.slice(0, 8)}] watchdog fired after ${idleFor}ms idle (limit ${idleTimeoutMs}ms) — marking failed`);if (mod?.cancel) mod.cancel(job.id);handlers.failed({ error: `watchdog timeout: no runner output for ${Math.round(idleFor / 1000)}s (limit ${Math.round(idleTimeoutMs / 1000)}s)` });@@ -877,6 +883,13 @@ export async function cancelJob(jobId) {?? codexRunning.find((j) => j.id === jobId)?? null;if (runningJob) {+ // A terminal transition already started (completed/failed/watchdog): its+ // async progress-drain leaves job.status === 'running' for a beat, but the+ // outcome is decided. Don't fire a redundant provider cancel or report+ // 'canceling' — treat it like an already-terminal job (route maps to 409).+ if (runningJob.terminating) {+ return { ok: false, code: 'ALREADY_TERMINAL', status: runningJob.status, error: 'Job is already finishing' };+ }// cancelRequested flips the dispatcher's `failed` handler into the// `canceled` branch instead of marking it failed.runningJob.cancelRequested = true;diff --git a/server/services/mediaJobQueue/index.test.js b/server/services/mediaJobQueue/index.test.jsindex 582f00ed21..06a82677c0 100644--- a/server/services/mediaJobQueue/index.test.js+++ b/server/services/mediaJobQueue/index.test.js@@ -278,6 +278,29 @@ describe('mediaJobQueue', () => {await waitFor(() => mediaJobQueue.getJob(job.jobId).status === 'completed');});+ it('cancel during the terminal drain window is refused, not "canceling"', async () => {+ const job = mediaJobQueue.enqueueJob({ kind: 'video', params: { prompt: 'finishing race' } });+ await waitFor(() => stubs.generateVideo.mock.calls.length === 1);++ // Dirty the debounce so terminate() must actually drain (the widened+ // async window this guards). emit() is synchronous: the 'completed'+ // handler runs terminate(), which sets job.terminating synchronously and+ // then parks on the async drain with job.status still 'running'.+ videoGenEvents.emit('progress', { generationId: job.jobId, progress: 0.9, message: 'Almost done' });+ videoGenEvents.emit('completed', { generationId: job.jobId, filename: `${job.jobId}.mp4` });++ // A cancel landing in that window must be refused (the outcome is already+ // decided) rather than reported as 'canceling', and must not fire a+ // redundant provider cancel. cancelJob's guard returns synchronously+ // before any await, so no microtask flips the status first.+ const result = await mediaJobQueue.cancelJob(job.jobId);+ expect(result.ok).toBe(false);+ expect(result.code).toBe('ALREADY_TERMINAL');+ expect(stubs.cancelVideo).not.toHaveBeenCalled();++ await waitFor(() => mediaJobQueue.getJob(job.jobId).status === 'completed');+ });+it('boot recovery: persisted "running" jobs are reclassified as failed', async () => {const interruptedId = '00000000-0000-4000-8000-000000000001';const persisted = {