The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
Runs: a result held for its output check is held once, by the app process saving it
Adarshd91ed40d418f
diff --git a/src/server/generations.ts b/src/server/generations.tsindex e01b7c41..ffa25a8d 100644--- a/src/server/generations.ts+++ b/src/server/generations.ts@@ -2685,7 +2685,7 @@ async function pollOne(r: Row): Promise<void> {}// The moderation service asked to see this run's outputs first: they are held, and the row waits ("Checking").if (stored.checkOutputs && moderationConfigured()) {- if (!holdForCheck(r.id, outputs, { live: true })) dropSaved();+ if (!holdForCheck(r.id, outputs, { live: true, turn: runnerToken() })) dropSaved();return;}const saved = updateLive(r.id, {@@ -2699,8 +2699,8 @@ async function pollOne(r: Row): Promise<void> {}/**- * Change a run the runner looked at, only while it still goes (queued or running) and no other app process holds its- * turn to save its results. A look that took a while (a slow poll, a deploy's overlap) never takes a run another+ * Change a run the runner looked at, only while it still goes (queued or running, and not held for its output check) and+ * no other app process holds its turn to save its results. A look that took a while (a slow poll, a deploy's overlap) never takes a run another* process saved, failed or canceled meanwhile back to an earlier state, and never fails a run another process is* saving. Returns whether the change was made.*/@@ -2710,7 +2710,7 @@ export function updateLive(id: string, fields: Partial<Row>, now = Date.now()):return (db().prepare(- `UPDATE generations SET ${keys.map((k) => `${k} = @${k}`).join(", ")}, updated_at = @__now WHERE id = @__id AND status IN ('queued', 'running')+ `UPDATE generations SET ${keys.map((k) => `${k} = @${k}`).join(", ")}, updated_at = @__now WHERE id = @__id AND status IN ('queued', 'running') AND stage IS NULLAND NOT EXISTS (SELECT 1 FROM generation_runs WHERE id = @__id AND finish_by IS NOT NULL AND finish_by != @__me AND finish_until >= @__now)`,).run({ ...fields, __now: now, __id: id, __me: runnerToken() }).changes > 0@@ -2736,8 +2736,9 @@ export function claimFinish(id: string, now = Date.now()): boolean {const d = db();return d.transaction((): boolean => {- const g = d.prepare("SELECT status FROM generations WHERE id = ?").get(id) as Pick<Row, "status"> | undefined;- if (!g || (g.status !== "queued" && g.status !== "running")) return false;+ const g = d.prepare("SELECT status, stage FROM generations WHERE id = ?").get(id) as Pick<Row, "status" | "stage"> | undefined;+ // A run held for its output check was saved already.+ if (!g || (g.status !== "queued" && g.status !== "running") || g.stage) return false;const me = runnerToken();d.prepare("INSERT OR IGNORE INTO generation_runs (id, submitted_at, started_at) VALUES (?, ?, NULL)").run(id, now);return (diff --git a/src/server/output-checks.ts b/src/server/output-checks.tsindex d48779d6..4592ea0e 100644--- a/src/server/output-checks.ts+++ b/src/server/output-checks.ts@@ -23,16 +23,22 @@ export const CHECKING_WORDS = "Checking";/*** Hold a finished run's files for their check (the runner calls it instead of marking the row succeeded). `live`: only- * while the run still goes (queued or running), as the runner saves it: a run stopped meanwhile stays stopped, nothing of- * it is held, and the caller removes its files. Returns whether the files were held.+ * while the run still goes (queued or running, not already held for its check), as the runner saves it: a run stopped+ * meanwhile stays stopped, nothing of it is held, and the caller removes its files. `turn`: the app process saving it+ * (generations.ts claimFinish); while another process holds the turn, that process saves the run, and nothing is held+ * here either. Returns whether the files were held.*/-export function holdForCheck(generationId: string, outputs: OutputFile[], o: { live?: boolean; now?: number } = {}): boolean {+export function holdForCheck(generationId: string, outputs: OutputFile[], o: { live?: boolean; turn?: string; now?: number } = {}): boolean {const now = o.now ?? Date.now();const d = db();const ids = outputs.map((o) => o.assetId).filter((x): x is string => !!x);- const live = o.live ? " AND status IN ('queued', 'running')" : "";+ const live = o.live ? " AND status IN ('queued', 'running') AND stage IS NULL" : "";+ const turn = o.turn ? " AND NOT EXISTS (SELECT 1 FROM generation_runs WHERE id = @id AND finish_by IS NOT NULL AND finish_by != @turn AND finish_until >= @now)" : "";return d.transaction((): boolean => {- const held = d.prepare(`UPDATE generations SET status = 'running', stage = 'checking', progress = 0.99, message = ?, outputs_json = ?, updated_at = ? WHERE id = ?${live}`).run(CHECKING_WORDS, JSON.stringify(outputs), now, generationId).changes > 0;+ const held =+ d+ .prepare(`UPDATE generations SET status = 'running', stage = 'checking', progress = 0.99, message = @message, outputs_json = @outputs, updated_at = @now WHERE id = @id${live}${turn}`)+ .run({ message: CHECKING_WORDS, outputs: JSON.stringify(outputs), now, id: generationId, ...(o.turn && { turn: o.turn }) }).changes > 0;if (!held) return false;for (const id of ids) {d.prepare("UPDATE assets SET held = 1 WHERE id = ?").run(id);diff --git a/tests/recovery-server.test.ts b/tests/recovery-server.test.tsindex d15d0b70..ee2659cf 100644--- a/tests/recovery-server.test.ts+++ b/tests/recovery-server.test.ts@@ -63,6 +63,7 @@ const pool = await import("@/server/computer/pool");const tasks = await import("@/server/computer/tasks");const billing = await import("@/server/billing");const outbox = await import("@/server/billing-outbox");+const outputChecks = await import("@/server/output-checks");const qs = await import("@/server/queue-settings");const { comfyProvider, loadManifest } = await import("@/server/providers/comfyui");const { loadIndex } = await import("@/server/gpu/index-file");@@ -1636,6 +1637,44 @@ describe("two app processes on one database (a deploy's overlap)", () => {}});+ it("with the moderation service checking outputs, a process whose turn runs out while it saves a result leaves it to the process that took over: nothing of its own save is held or checked", async () => {+ const mod = await startFakeModeration();+ process.env.FUOBE_MODERATION_URL = mod.url;+ mod.run = { allowed: true, checkOutputs: true };+ try {+ const w = standInWorker();+ const { id } = await pressRun();+ const d = driver(w);+ // As its file is kept, the save here has taken past its turn, and another process took the turn over.+ db().exec(`CREATE TEMP TRIGGER turn_taken AFTER INSERT ON assets WHEN NEW.generation_id = '${id}' BEGIN UPDATE generation_runs SET finish_by = 'another-process', finish_until = ${Date.now() + 10 * MIN} WHERE id = '${id}'; END`);+ try {+ await workAll(d);+ await until(() => taskOf(id)?.state === "done" && assetsOf(id) === 0);+ for (let i = 0; i < 5; i++) await tick();+ } finally {+ db().exec("DROP TRIGGER IF EXISTS turn_taken");+ }+ // Nothing of this process's save is held or checked: its file went.+ expect(getGeneration(id)?.status).toBe("running");+ expect((db().prepare("SELECT stage FROM generations WHERE id = ?").get(id) as { stage: string | null }).stage).toBeNull();+ expect((db().prepare("SELECT COUNT(*) AS n FROM output_checks WHERE generation_id = ?").get(id) as { n: number }).n).toBe(0);+ expect(assetsOf(id)).toBe(0);+ // A slow look here changes nothing while the other process saves it.+ expect(updateLive(id, { status: "failed", error: "Timed out after 45 minutes", completed_at: Date.now() })).toBe(false);+ // The process holding the turn holds its own save for the check (here a result with no file, which succeeds as it is),+ // once: a run already held for its check is never held again.+ db().prepare("UPDATE generations SET stage = 'checking' WHERE id = ?").run(id);+ expect(outputChecks.holdForCheck(id, [], { live: true, turn: "another-process" })).toBe(false);+ db().prepare("UPDATE generations SET stage = NULL WHERE id = ?").run(id);+ expect(outputChecks.holdForCheck(id, [], { live: true, turn: "a-third-process" })).toBe(false);+ expect(outputChecks.holdForCheck(id, [], { live: true, turn: "another-process" })).toBe(true);+ expect(getGeneration(id)?.status).toBe("succeeded");+ } finally {+ delete process.env.FUOBE_MODERATION_URL;+ await mod.close();+ }+ });+it("a look at a run that comes back after another process saved it, or while another process saves it, changes nothing", async () => {const w = standInWorker();const { id } = await pressRun();