Skip to content

armgit / fuobe

Public Imported from GitHub
0

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.

Runs: a slow look at a run never undoes another app process's save

Adarshd059936302b6
diff --git a/src/server/generations.ts b/src/server/generations.ts
index d833d602..df0dd8a8 100644
--- a/src/server/generations.ts
+++ b/src/server/generations.ts
@@ -2535,7 +2535,7 @@ async function pollOne(r: Row): Promise<void> {
await adapterFor(r.provider, account)
.cancel?.(ctx)
.catch(() => {});
- update(r.id, {
+ updateLive(r.id, {
status: "failed",
error: run.started_at || deadline
? `Timed out after ${Math.round(limitMs / 60_000)} minutes`
@@ -2554,24 +2554,24 @@ async function pollOne(r: Row): Promise<void> {
if (!res) throw pollError;
if (startsNow) db().prepare("UPDATE generation_runs SET started_at = ? WHERE id = ?").run(Date.now(), r.id);
// Back in line (a worker paused, went quiet or gave it back): no progress of the attempt before is kept.
- update(r.id, { status: res.state, progress: ownClocks && res.state === "queued" ? null : (res.progress ?? r.progress), message: res.message ?? null });
+ updateLive(r.id, { status: res.state, progress: ownClocks && res.state === "queued" ? null : (res.progress ?? r.progress), message: res.message ?? null });
return;
}
if (res.state === "failed") {
// What the GPU was missing (a model, a node class): the tile offers "Fix it", and the GPU's check counts as stale.
- update(r.id, { status: "failed", error: res.error, completed_at: Date.now(), message: null, ...(res.fix && { fix_json: JSON.stringify(res.fix) }) });
+ updateLive(r.id, { status: "failed", error: res.error, completed_at: Date.now(), message: null, ...(res.fix && { fix_json: JSON.stringify(res.fix) }) });
// (A run on this install's GPUs says nothing about the account's own GPU.)
if (res.fix && !onPool) markStale(account ? `acct:${account}` : "install");
return;
}
// The Watermark Remover gave the original back (no clean repair was possible): no new result, the run says what to try.
if (r.op === "watermark-remove" && (res.outputs as RichOutput[]).some((o) => rejectedRepair(o.meta?.quality as WatermarkQuality | undefined))) {
- update(r.id, { status: "failed", error: WATERMARK_COPY.rejected, completed_at: Date.now(), message: null });
+ updateLive(r.id, { status: "failed", error: WATERMARK_COPY.rejected, completed_at: Date.now(), message: null });
return;
}
// A Watermark Remover scan with no report the page can read fails in words (never a page that waits for marks forever).
if (r.op === "watermark-scan" && !(res.outputs as RichOutput[]).some((o) => parseScanReport(o.text))) {
- update(r.id, { status: "failed", error: WATERMARK_COPY.scanFailed, completed_at: Date.now(), message: null });
+ updateLive(r.id, { status: "failed", error: WATERMARK_COPY.scanFailed, completed_at: Date.now(), message: null });
return;
}
// A Watermark Remover result keeps how it was asked, next to the repair's verdict: the marks removed (as checked) and how.
@@ -2591,7 +2591,7 @@ async function pollOne(r: Row): Promise<void> {
// deploy's overlap never both save them, and the second never ends a saved run as failed).
if (!claimFinish(r.id)) return;
// Mark first so a slow download isn't polled twice.
- update(r.id, { status: "running", progress: 0.99, message: "Saving results" });
+ updateLive(r.id, { status: "running", progress: 0.99, message: "Saving results" });
const outputs: OutputFile[] = [];
// An account's job keeps at most 16 outputs, and a result it can't save fails the job (never a link to its ComfyUI).
const dropped = account && res.outputs.length > ACCOUNT_LIMITS.outputsPerJob ? res.outputs.length - ACCOUNT_LIMITS.outputsPerJob : 0;
@@ -2604,7 +2604,7 @@ async function pollOne(r: Row): Promise<void> {
try {
outputs.push(await mosaicOutput(r, res.outputs as RichOutput[]));
} catch (e) {
- update(r.id, { status: "failed", error: (e as Error).message, completed_at: Date.now(), message: null });
+ updateLive(r.id, { status: "failed", error: (e as Error).message, completed_at: Date.now(), message: null });
return;
}
} else {
@@ -2622,7 +2622,7 @@ async function pollOne(r: Row): Promise<void> {
if (left) fs.rmSync(left, { force: true });
}
const why = e instanceof AccountFilesFull ? e.message : "We couldn't save this result. Press Retry.";
- update(r.id, { status: "failed", error: why, completed_at: Date.now(), message: null });
+ updateLive(r.id, { status: "failed", error: why, completed_at: Date.now(), message: null });
return;
}
outputs.push({ url: o.url, kind: o.kind ?? r.kind, ...inlineText(o.text, o.meta), ...(o.draftId ? { draftId: o.draftId } : {}) });
@@ -2640,7 +2640,7 @@ async function pollOne(r: Row): Promise<void> {
}
for (const o of cards) deleteAsset(o.assetId!);
if (cards.size && cards.size === outputs.length) {
- update(r.id, { status: "failed", error: BLOCKED_CARD_MESSAGE, completed_at: Date.now(), message: null });
+ updateLive(r.id, { status: "failed", error: BLOCKED_CARD_MESSAGE, completed_at: Date.now(), message: null });
return;
}
if (cards.size) outputs.splice(0, outputs.length, ...outputs.filter((o) => !cards.has(o)));
@@ -2651,7 +2651,7 @@ async function pollOne(r: Row): Promise<void> {
if (!measured) {
// Nothing of a failed build is kept, the owner's included.
for (const kept of outputs) if (kept.assetId) deleteAsset(kept.assetId);
- update(r.id, { status: "failed", error: NO_SUBJECT_MESSAGE, completed_at: Date.now(), message: null });
+ updateLive(r.id, { status: "failed", error: NO_SUBJECT_MESSAGE, completed_at: Date.now(), message: null });
return;
}
outputs.splice(0, outputs.length, ...measured);
@@ -2670,7 +2670,7 @@ async function pollOne(r: Row): Promise<void> {
// stored now and the row stays running until it's done, so a restart resumes it from them. The turntable
// is rendered after the job has succeeded (src/server/model3d-finish.ts).
if (modelToFinish(r, outputs) >= 0) {
- update(r.id, { outputs_json: JSON.stringify(outputs), external_meta_json: JSON.stringify({ ...ctx.meta, finishing: true }), message: "Finishing the 3D model" });
+ updateLive(r.id, { outputs_json: JSON.stringify(outputs), external_meta_json: JSON.stringify({ ...ctx.meta, finishing: true }), message: "Finishing the 3D model" });
resumeFinish({ ...r, outputs_json: JSON.stringify(outputs) });
return;
}
@@ -2679,7 +2679,7 @@ async function pollOne(r: Row): Promise<void> {
holdForCheck(r.id, outputs);
return;
}
- update(r.id, {
+ updateLive(r.id, {
status: "succeeded",
outputs_json: JSON.stringify(outputs),
progress: 1,
@@ -2688,6 +2688,25 @@ 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
+ * 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.
+ */
+export function updateLive(id: string, fields: Partial<Row>, now = Date.now()): boolean {
+ const keys = Object.keys(fields);
+ if (!keys.length) return false;
+ return (
+ db()
+ .prepare(
+ `UPDATE generations SET ${keys.map((k) => `${k} = @${k}`).join(", ")}, updated_at = @__now WHERE id = @__id AND status IN ('queued', 'running')
+ AND 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
+ );
+}
+
/** How long an app process holds the turn to save a finished run's results (it renews it as it goes). */
const FINISH_TURN_MS = 5 * 60_000;
diff --git a/tests/recovery-server.test.ts b/tests/recovery-server.test.ts
index c45f6ae3..f6cee304 100644
--- a/tests/recovery-server.test.ts
+++ b/tests/recovery-server.test.ts
@@ -57,7 +57,7 @@ const { NextRequest } = await import("next/server");
const { db } = await import("@/server/db");
const { resetRates } = await import("@/server/rate");
const { setSetting } = await import("@/server/settings");
-const { getGeneration, ensureRunner } = await import("@/server/generations");
+const { getGeneration, ensureRunner, updateLive } = await import("@/server/generations");
const pool = await import("@/server/computer/pool");
const tasks = await import("@/server/computer/tasks");
const billing = await import("@/server/billing");
@@ -1215,6 +1215,31 @@ describe("two app processes on one database (a deploy's overlap)", () => {
await expectOnce(id, watch, { prompts: 1, place: 1 });
});
+ 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();
+ const watch = watchOf(id);
+ const d = driver(w);
+ lookAtEveryStep(d, watch);
+ await workAll(d);
+ await expectOnce(id, watch, { prompts: 1, place: 1 });
+ const saved = getGeneration(id)!;
+ // A slow look from before the save says it still works, or that it failed: the saved run stays as it is.
+ expect(updateLive(id, { status: "running", progress: 0.5, message: POOL_WORDS.working })).toBe(false);
+ expect(updateLive(id, { status: "failed", error: "Timed out after 45 minutes", completed_at: Date.now() })).toBe(false);
+ expect(getGeneration(id)).toMatchObject({ status: "succeeded", outputs: saved.outputs });
+ // A run another process is saving now: a look here neither fails it nor moves it back.
+ const next = (await pressRun()).id;
+ db().prepare("UPDATE generation_runs SET finish_by = 'another-process', finish_until = ? WHERE id = ?").run(Date.now() + 60_000, next);
+ expect(updateLive(next, { status: "failed", error: "Timed out after 45 minutes", completed_at: Date.now() })).toBe(false);
+ expect(getGeneration(next)?.status).toBe("queued");
+ // Its turn runs out (that process stopped): this one may change it again.
+ db().prepare("UPDATE generation_runs SET finish_until = ? WHERE id = ?").run(Date.now() - 1, next);
+ expect(updateLive(next, { message: POOL_WORDS.inLine })).toBe(true);
+ await workAll(d);
+ expect((await ended(next))?.status).toBe("succeeded");
+ });
+
it("both processes' sweeps meet over a finished run's report: one sends it, so the service gets it once", async () => {
const w = standInWorker();
const { id } = await pressRun();