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
Workers: a job its ComfyUI loses goes back in line, and only lost attempts count across claims
Adarsh5bf1bed8f295
diff --git a/src/server/computer/pool.ts b/src/server/computer/pool.tsindex 24080560..8989d562 100644--- a/src/server/computer/pool.ts+++ b/src/server/computer/pool.ts@@ -5,7 +5,7 @@ import { RELAY_TIMES, SENT_STATES, type BeatAnswer, type ClaimedTask, type Compuimport { clock, hoursAt, parseHours, type Hours } from "@/lib/worker-hours";import { db } from "../db";import { loadIndex } from "../gpu/index-file";-import { cleanFacts, endTask, json, logWorkerJob, noteProgress, setPoolClocks, setPoolGiveBack, setTask, takeTask, taskById, type TaskRow } from "./tasks";+import { cleanFacts, endTask, json, logWorkerJob, noteProgress, setPoolClocks, setPoolGiveBack, setPoolLost, setTask, takeTask, taskById, type TaskRow } from "./tasks";import { queueSettings, runCeilingMs } from "../queue-settings";import { createWorker, offerNextToken, revokeWorker, rotateWorker, type WorkerRow } from "./workers";@@ -559,6 +559,12 @@ setPoolGiveBack((t, why) => {backInLine(t, { blame: true, why: GAVE_BACK_WORDS });});+/**+ * A worker's ComfyUI lost its task twice on one claim (each loss sends it again once): it goes back in line with its+ * place kept, and counts as an attempt, so another worker is preferred and the third attempt fails it.+ */+setPoolLost((t) => backInLine(t, { blame: true }));+// ---------------------------------------------------------------------------// Place in line and the wait (4.4)diff --git a/src/server/computer/tasks.ts b/src/server/computer/tasks.tsindex 2c9ae13b..58830caa 100644--- a/src/server/computer/tasks.ts+++ b/src/server/computer/tasks.ts@@ -530,7 +530,9 @@ export function takeTask(t: TaskRow, device: string, now: number, fields: Partiaconst lease = crypto.randomBytes(32).toString("hex");const resume = t.state === "waiting" || t.device !== device ? undefined : (t.state as ClaimedTask["resume"]);const fresh = t.state === "waiting" || t.device !== device;- setTask(t.id, { device, lease_hash: hash(lease), state: t.state === "waiting" ? "sending" : t.state, claimed_at: now, device_seen_at: now, ...fields });+ // A worker's task taken afresh starts its own count of sends: its ComfyUI may lose it once and get it again on this+ // claim (the report "gone"). Across claims, only its requeues count (pool.ts MAX_ATTEMPTS).+ setTask(t.id, { device, lease_hash: hash(lease), state: t.state === "waiting" ? "sending" : t.state, claimed_at: now, device_seen_at: now, ...(fresh && t.pools && { attempts: 0 }), ...fields });if (fresh) rdb().prepare("UPDATE relay_inputs SET reads = 0 WHERE task_id = ?").run(t.id);const inputs: TaskInput[] = inputsOf(t.id).map((i) => ({ n: i.n!, name: i.name, size: i.size, sha256: i.sha256, contentType: i.content_type }));return {@@ -719,6 +721,12 @@ function reportOnce(t: TaskRow, e: ReportEvent, plan: (entries: HistoryEntryLikereturn { ok: true };}if (t.state === "sending") return { ok: true };+ // A worker's ComfyUI lost it a second time on this claim: back in line as a lost attempt, with its place kept (the+ // line's one budget: pool.ts MAX_ATTEMPTS).+ if (t.pools && poolLost) {+ poolLost(t);+ return { ok: true };+ }endTask(t.id, "failed", { error: "lost" });return { ok: true };case "comfy-down":@@ -919,6 +927,10 @@ export const setPoolClocks = (fn: (t: TaskRow, now: number) => TaskRow) => (poollet poolGiveBack: ((t: TaskRow, why: GiveBackWhy) => void) | null = null;export const setPoolGiveBack = (fn: (t: TaskRow, why: GiveBackWhy) => void) => (poolGiveBack = fn);+/** A worker's ComfyUI lost its task again: it goes back in line as a lost attempt (pool.ts sets this too). */+let poolLost: ((t: TaskRow) => void) | null = null;+export const setPoolLost = (fn: (t: TaskRow) => void) => (poolLost = fn);+/** The execution time (ms) and GPU name a worker reported with a finished job, as the workers' log keeps them. */export interface ExecFacts {ms: number;diff --git a/tests/worker-queue.test.ts b/tests/worker-queue.test.tsindex 2a375b6a..8c201fa3 100644--- a/tests/worker-queue.test.ts+++ b/tests/worker-queue.test.ts@@ -139,6 +139,72 @@ describe("one job at a time", () => {});});+describe("a job its worker's ComfyUI loses", () => {+ const plan = () => ({ error: "unused" });+ /** The worker sends the job to its ComfyUI, which starts it. */+ const sendAndRun = (id: string) => {+ tasks.report(rowOf(id), { event: "sent", promptId: crypto.randomUUID() }, plan);+ tasks.report(rowOf(id), { event: "running", value: 1, max: 4 }, plan);+ };+ const lose = (id: string) => tasks.report(rowOf(id), { event: "gone" }, plan);++ it("after a pause put it back in line, one loss on the next claim sends it again, and no attempt is used", () => {+ const w = standInWorker();+ const a = accountTask();+ beatAs(w);+ expect(claimAs(w)?.id).toBe(a.id);+ sendAndRun(a.id);+ // The worker pauses at once: the job goes back in line at no cost.+ beatAs(w, Date.now(), { paused: true, why: "here-now" }, [{ task: a.id }]);+ expect(rowOf(a.id).state).toBe("waiting");+ beatAs(w);+ expect(claimAs(w)?.id).toBe(a.id);+ sendAndRun(a.id);+ // Its ComfyUI restarts once: the worker sends it again on this claim.+ expect(lose(a.id).state).toBe("sending");+ expect(rowOf(a.id).requeues).toBe(0);+ });++ it("lost by a worker that went quiet, then once by another worker's ComfyUI: it sends again there", () => {+ const first = standInWorker("first");+ const a = accountTask();+ beatAs(first);+ expect(claimAs(first)?.id).toBe(a.id);+ sendAndRun(a.id);+ db().prepare("UPDATE relay_workers SET seen_at = ? WHERE id = ?").run(Date.now() - 3 * MIN, first.id);+ pool.sweepPool();+ expect(rowOf(a.id).state).toBe("waiting");+ expect(rowOf(a.id).requeues).toBe(1);+ const second = standInWorker("second");+ beatAs(second);+ expect(claimAs(second)?.id).toBe(a.id);+ sendAndRun(a.id);+ expect(lose(a.id).state).toBe("sending");+ expect(rowOf(a.id).requeues).toBe(1);+ });++ it("lost twice on one claim: back in line with its place as one attempt, and the third such attempt fails it", () => {+ const w = standInWorker();+ const a = accountTask({ rankAt: Date.now() - 5 * MIN });+ const rank = rowOf(a.id).rank_at;+ for (let i = 1; i <= pool.MAX_ATTEMPTS; i++) {+ beatAs(w);+ expect(claimAs(w)?.id).toBe(a.id);+ sendAndRun(a.id);+ expect(lose(a.id).state).toBe("sending");+ sendAndRun(a.id);+ const answer = lose(a.id);+ if (i < pool.MAX_ATTEMPTS) {+ expect(answer.state).toBe("waiting");+ expect(rowOf(a.id).requeues).toBe(i);+ expect(rowOf(a.id).rank_at).toBe(rank);+ }+ }+ expect(rowOf(a.id).state).toBe("failed");+ expect(rowOf(a.id).error).not.toBe("lost");+ });+});+describe("back in line", () => {it("a requeue keeps the job's place (rank_at), and once a worker took it, its wait limit is over", () => {const w = standInWorker();diff --git a/tests/workers-pool.test.ts b/tests/workers-pool.test.tsindex c9cdf134..6ec5ad81 100644--- a/tests/workers-pool.test.ts+++ b/tests/workers-pool.test.ts@@ -760,8 +760,10 @@ describe("a worker's own reports: giving a job back, its execution time, the inpexpect(state(id).attempts).toBe(1);pool.backInLine(state(id), { blame: true });const again = await routed(w, id);+ // A new claim starts its own count of sends, and results kept from an earlier try add none.+ expect(state(id).attempts).toBe(0);expect((await again.say({ event: "sent", promptId: again.t.promptId, kept: true })).status).toBe(200);- expect(state(id).attempts).toBe(1);+ expect(state(id).attempts).toBe(0);const done = await again.say({ event: "finished", entry, execMs: 4 * MIN, device: "Test GPU <b>", kept: true });expect(done.status).toBe(200);expect(state(id).state).not.toBe("queued");