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 on the studio's GPUs: a run whose first send was cut off before its job was made is sent again in its place
Adarshfdd390805043
diff --git a/docs/gpu-workers.md b/docs/gpu-workers.mdindex cbd86caf..590fe216 100644--- a/docs/gpu-workers.md+++ b/docs/gpu-workers.md@@ -657,7 +657,10 @@ job is made; when the process that was sending it stopped part way, the next onemade, and the step is sent now (in its run's place); one made, and the step ends in the one plain sentence and thatjob stops, so it is never run twice. That look is for steps on the workers, whose jobs are tasks in the studio'sdatabase. A step sent to the install's own ComfyUI (on the studio's machine) may be in its queue already, so when its-send was cut off it ends in the plain sentence instead of being sent again. When the run is canceled or deleted, its finishing jobs end too: one in line at once,+send was cut off it ends in the plain sentence instead of being sent again. A run's first job is the same: a run whose+first send was cut off (a restart or a deploy) before its job was made on the workers is sent again by one app process,+in the place it had (a run's first job waits from the time the run was made), instead of failing; one whose job was+made, or that went to the install's own ComfyUI, ends in the plain sentence. When the run is canceled or deleted, its finishing jobs end too: one in line at once,with its files, and one that a CPU slot runs at its next progress report (the worker sends one at least every 10seconds). A 3D model finished this way waits for the output check when the run asked for one, as any run's outputs do.The time a CPU task runs is never counted as GPU time.diff --git a/src/server/computer/pool.ts b/src/server/computer/pool.tsindex b6ac5be9..44cdb5d6 100644--- a/src/server/computer/pool.ts+++ b/src/server/computer/pool.ts@@ -1028,8 +1028,9 @@ const SAME_PRESS = "(CASE WHEN @hold IS NOT NULL THEN json_extract(g.request_jso** - `class`: the run's ("paid" when the billing service said so, else "free").* - `rankAt`: the run's place in line. The first job any of its rows had on the workers sets it (the row itself, or a- * step of the same run that fed it). A later step with no such job yet keeps the time the run was made (less its head- * start). Null for a run's first job: it waits from now.+ * step of the same run that fed it). A row with no such job yet keeps the time the run was made (less its head start):+ * a run's first job waits in the order it came, and so does one sent again after its first send was cut off before+ * its job was made (a restart or a deploy: generations.ts sendAgainOnWorkers).* - `started`: a worker took one of those jobs. The run's wait limit no longer applies then (it applies only before the* run's first start).*@@ -1063,7 +1064,7 @@ export function runPlaceOf(generationId: string): { class: "paid" | "free"; rankconst boost = Math.max(0, Math.min(Number(g.boost) || 0, 24 * 60 * 60_000));return {class: g.cls === "paid" ? "paid" : "free",- rankAt: jobs[0]?.rank_at ?? (g.depends_on ? g.created_at - boost : null),+ rankAt: jobs[0]?.rank_at ?? g.created_at - boost,started: jobs.some((t) => t.claimed_at !== null),};}diff --git a/src/server/generations.ts b/src/server/generations.tsindex 5ec71863..420df637 100644--- a/src/server/generations.ts+++ b/src/server/generations.ts@@ -3345,19 +3345,23 @@ function startRunner(): void {/*** Runs with no job yet that no app process that still runs is handing over (takeSubmitTurn: its process stopped, by a* restart, or a deploy's old instance gone). One waiting for its own GPU or key goes on here, with what is left of its- * wait (one process takes it); one cut off in the middle of its hand-over can't be recovered, and fails. A run a process- * that still runs is sending or waiting on (this one, or another in a deploy's overlap) is left to it. Runs at start and- * every tick; not while this server stops (the app process that goes on looks).+ * wait (one process takes it); so does one on this install's workers whose job was never made (sendAgainOnWorkers). One+ * cut off in the middle of its hand-over can't be recovered, and fails. A run a process that still runs is sending or+ * waiting on (this one, or another in a deploy's overlap) is left to it. Runs at start and every tick; not while this+ * server stops (the app process that goes on looks).*/function sweepInterrupted(): void {if (draining()) return;const now = Date.now();const rows = db().prepare(- `SELECT g.id, g.provider, g.model_id, g.op, g.request_json, g.external_meta_json FROM generations g+ `SELECT g.id, g.provider, g.model_id, g.op, g.request_json, g.external_meta_json, g.batch_id, g.created_at FROM generations gWHERE g.status IN ('queued','running') AND g.external_id IS NULL AND g.depends_on IS NULL AND g.created_at < @before AND NOT ${TURN_HELD("g", false)}`,)- .all({ before: now - STALE_SUBMIT.ms, __me: runnerToken(), __quiet: now - QUIET_MS, __now: now }) as Pick<Row, "id" | "provider" | "model_id" | "op" | "request_json" | "external_meta_json">[];+ .all({ before: now - STALE_SUBMIT.ms, __me: runnerToken(), __quiet: now - QUIET_MS, __now: now }) as Pick<+ Row,+ "id" | "provider" | "model_id" | "op" | "request_json" | "external_meta_json" | "batch_id" | "created_at"+ >[];const busy = submitting();for (const r of rows) {if (busy.has(r.id)) continue;@@ -3366,7 +3370,7 @@ function sweepInterrupted(): void {if (takeOver(r, meta.heal)) continue;// (As it is now: the process it was taken from may have marked it as being sent since this look.)meta = readMeta(db().prepare("SELECT external_meta_json FROM generations WHERE id = ?").pluck().get(r.id) as string | null);- }+ } else if (sendAgainOnWorkers(r)) continue;// A run that waited and was cut off in the middle of its send may have reached its GPU or provider: never sent twice.const cutOff = !!meta.heal && typeof meta.heal.sending === "number";const words = cutOff ? CUT_OFF_SENDING : "This run stopped before it started. Press Retry.";@@ -3382,6 +3386,41 @@ function sweepInterrupted(): void {/** A waiting run whose app process was cut off while it sent the job (its GPU or provider may have it): not sent again. */const CUT_OFF_SENDING = "This run was interrupted while it was being sent. Press Retry.";+/**+ * A run on this install's workers whose hand-over was cut off before its job was made (a restart or a deploy while its+ * inputs were being made ready): its job would be a task of this database, and there is none, so nothing reached a+ * worker. It is taken by this process (one process takes it) and sent now as it was decided (its stamp: its place in+ * line, its class, its hold), as the same job of its batch, instead of failing. Its job waits from the time the run was+ * made (pool.ts runPlaceOf), so it keeps its place. False: not one of those, and it fails as before (one on the install's+ * own ComfyUI may be in that ComfyUI's queue already; one whose job was made may run, and stops with it).+ */+function sendAgainOnWorkers(r: Pick<Row, "id" | "provider" | "request_json" | "external_meta_json" | "batch_id" | "created_at">): boolean {+ if (r.provider !== "comfyui" || !poolOnWorkers() || hasJobFor(r.id)) return false;+ let stored: GenerationRequest;+ try {+ stored = JSON.parse(r.request_json) as GenerationRequest;+ } catch {+ return false;+ }+ const account = stored.account;+ if (!account || stored.place !== "pool") return false;+ // Another app process took it first.+ if (!takeSubmitTurn(r.id)) return true;+ try {+ // Looked at again under the turn: a job the process that held it made meanwhile (it stalled rather than stopped).+ if (hasJobFor(r.id)) throw new LetGo("Its job was made");+ const p = runOnPool(account, () => withRunStamp(stampOf(stored), () => prepare(stored)));+ if (p.providerId !== r.provider) throw new LetGo("Planned elsewhere now");+ submitting().add(r.id);+ void submit(r.id, p.route, p.providerId, p.plan, p.req, jobIndexOf(r, readMeta(r.external_meta_json)));+ return true;+ } catch {+ // It fails as before (the sweep's look, once this process has given its turn back).+ handOverEnds(r.id);+ return false;+ }+}+/*** A waiting run whose turn no app process that still runs holds: taken by this one, planned again as its account runs now* (its GPU, its keys), and its wait goes on where it was (its budget and its tries kept). False: it isn't one this candiff --git a/tests/recovery-server.test.ts b/tests/recovery-server.test.tsindex f551cf17..d457968f 100644--- a/tests/recovery-server.test.ts+++ b/tests/recovery-server.test.ts@@ -1754,6 +1754,77 @@ describe("two app processes on one database (a deploy's overlap)", () => {expect(jobsOf(step)).toHaveLength(0);});+ /**+ * What a process killed in the middle of a run's first send leaves behind: the run made minutes ago, no job id on it,+ * its turn and its lease with that process (quiet for longer than 20 s). `job`: it had made the run's job, and was killed+ * before it noted it on the run; else no job was made. The time the run was made.+ */+ function cutOffFirstSend(id: string, token: string, o: { job?: boolean } = {}): number {+ const made = Date.now() - 5 * MIN;+ if (!o.job) db().prepare("DELETE FROM relay_tasks WHERE generation_id = ?").run(id);+ db().prepare("DELETE FROM generation_runs WHERE id = ?").run(id);+ db()+ .prepare("UPDATE generations SET external_id = NULL, external_meta_json = json_object('lease', json_object('by', ?, 'until', ?)), message = 'Submitting', created_at = ? WHERE id = ?")+ .run(token, Date.now() + 30_000, made, id);+ sendingIn(token, id, QUIET_MS + 1000);+ return made;+ }++ it("a run whose first send was cut off before its job was made is sent once by one of them, in its place in line, and its output arrives with no error", async () => {+ const w = standInWorker();+ const d = driver(w);+ const { id } = await pressRun();+ const made = cutOffFirstSend(id, "app-cut-off-run");+ // Another account's run, pressed after it, waits in line meanwhile.+ const { id: later } = await pressRun(IMAGE, b1);+ await Promise.all([tick(), app2.tick()]);+ for (let i = 0; i < 200 && !jobsOf(id).length; i++) await sleep(25);+ await sleep(1000);+ await Promise.all([tick(), app2.tick()]);+ expect(jobsOf(id), "one job for it on the workers").toHaveLength(1);+ // It waits from the time it was made: before the run pressed after it.+ expect(jobsOf(id)[0].rank_at).toBe(made);+ expect(jobsOf(id)[0].rank_at!).toBeLessThan(taskOf(later)!.rank_at!);+ const watch = watchOf(id, { "other process": there(id) });+ await watch.look();+ expect(watch.path()).toEqual(["queued"]);+ await workAll(d);+ await expectOnce(id, watch, { prompts: 1, place: 1 });+ expect(jobsOf(id)).toHaveLength(1);+ expect((await ended(later))?.status).toBe("succeeded");+ });++ it("a run whose first send was cut off after its job was made is never sent twice: it ends in plain words, and that job stops", async () => {+ standInWorker();+ const { id, task } = await pressRun();+ cutOffFirstSend(id, "app-cut-off-run-2", { job: true });+ await Promise.all([tick(), app2.tick()]);+ await expectFreeFailure(id);+ await Promise.all([tick(), app2.tick()]);+ expect(jobsOf(id).map((t) => t.id), "nothing more was sent").toEqual([task.id]);+ expect(jobsOf(id)[0].state, "the job it made stops with it").toBe("canceled");+ });++ it("a run on the install's own ComfyUI whose first send was cut off is never sent again: it ends in plain words, and nothing more is posted", async () => {+ onInstallServer();+ const posts = comfy.posts();+ const r = await run(IMAGE);+ expect(r.status, JSON.stringify(r.body)).toBe(201);+ const id = r.body.items![0].id;+ // Its job reached that ComfyUI (no task here), and the process that posted it was killed before it noted it on the run.+ const noted = () => (db().prepare("SELECT external_id FROM generations WHERE id = ?").get(id) as { external_id: string | null }).external_id;+ for (let i = 0; i < 200 && !(comfy.posts() > posts && noted()); i++) await sleep(25);+ expect(comfy.posts()).toBeGreaterThan(posts);+ cutOffFirstSend(id, "app-cut-off-run-3");+ const sent = comfy.posts();+ await Promise.all([tick(), app2.tick()]);+ await expectFreeFailure(id);+ await Promise.all([tick(), app2.tick()]);+ await sleep(500);+ expect(comfy.posts(), "nothing more was posted to that ComfyUI").toBe(sent);+ expect(jobsOf(id)).toHaveLength(0);+ });+it("a step and a preview picture the worker reports to the other process are seen here too: a refresh, both sessions, the list and the other process show the same step", async () => {const w = standInWorker();const { id } = await pressRun();