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.

Worker: a retry after running out of memory that freezes goes to another worker sooner

Adarshc16c8755e3e9
diff --git a/src/lib/computer-runner.ts b/src/lib/computer-runner.ts
index 64b2b0dc..7fa41135 100644
--- a/src/lib/computer-runner.ts
+++ b/src/lib/computer-runner.ts
@@ -302,6 +302,12 @@ export const MIN_RUN_LIMIT_MS = 10 * 60_000;
/** The shortest stall limit a worker takes from a claim (a shorter one would give back jobs between two looks). */
export const MIN_STALL_MS = 60_000;
+/**
+ * A worker's retry after its ComfyUI ran out of memory that doesn't move for this long (or runs past the run limit) is
+ * given back as out of memory: the job goes to another worker instead of waiting out the whole stall limit here.
+ */
+export const OOM_RETRY_STALL_MS = 5 * 60_000;
+
const PROGRESS_EVERY_MS = 500;
/** A finishing job tells the studio it still runs at least this often (and hears a stop in the answer). */
const FINISH_BEAT_MS = 10_000;
@@ -1346,8 +1352,11 @@ export class ComputerRunner {
for (;;) {
if (!this.alive) throw new Stopped();
if (worker) {
- if (runLimit && runningSince !== null && this.now() - runningSince > runLimit) return { giveBack: "run-limit" };
- if (this.now() - movedAt > stallMs) return { giveBack: "stalled" };
+ const over = !!runLimit && runningSince !== null && this.now() - runningSince > runLimit;
+ const still = this.now() - movedAt;
+ if (oomRetried && (over || still > Math.min(stallMs, OOM_RETRY_STALL_MS))) return { giveBack: "out-of-memory" };
+ if (over) return { giveBack: "run-limit" };
+ if (still > stallMs) return { giveBack: "stalled" };
}
if (following) {
// A meta-batch: its passes run under new ids, all with its prefix.
diff --git a/src/lib/relay.ts b/src/lib/relay.ts
index 397abdfb..2cc665b1 100644
--- a/src/lib/relay.ts
+++ b/src/lib/relay.ts
@@ -45,7 +45,7 @@ export type TabFailure = (typeof TAB_FAILURES)[number];
* nothing too: the worker's own check before the run failed ("self-check": its face detector read the fixed photo wrong,
* bin/lib/worker/self-check.mjs), and the worker pauses itself.
*/
-export const GIVE_BACK = ["stalled", "run-limit", "media", "update-worker", "outside-ceiling", "self-check"] as const;
+export const GIVE_BACK = ["stalled", "run-limit", "media", "update-worker", "outside-ceiling", "self-check", "out-of-memory"] as const;
export type GiveBackWhy = (typeof GIVE_BACK)[number];
/**
diff --git a/src/server/computer/pool.ts b/src/server/computer/pool.ts
index ba53520c..71864608 100644
--- a/src/server/computer/pool.ts
+++ b/src/server/computer/pool.ts
@@ -1447,14 +1447,15 @@ setPoolGiveBack((t, why) => {
// anything ran on that claim, it waits as it did before that claim (a task no worker took keeps its wait limit). Once
// a job of it was sent or ran, it keeps its claim's time: its wait limit is over for good. In one locked step with
// going back in line, so a claim another app process makes meanwhile is never changed.
- if (why === "update-worker" || why === "outside-ceiling") {
+ // Ran out of memory here, and its retry here froze too: the same, except that it ran (it counts as an attempt).
+ if (why === "update-worker" || why === "outside-ceiling" || why === "out-of-memory") {
db()
.transaction(() => {
const w = t.worker_id ? (db().prepare("SELECT id, catalog_version, ceiling_json FROM relay_workers WHERE id = ?").get(t.worker_id) as Pick<WorkerRow, "id" | "catalog_version" | "ceiling_json"> | undefined) : undefined;
const { claimedBefore, claimFor, ...rest } = json<Record<string, unknown>>(t.extra_json, {});
const extra = { ...rest, refusedBy: [...new Set([...refusedBy(t), ...(w ? [refusalKey(w)] : [])])].slice(-64) };
const untouched = typeof claimFor === "number" && claimFor === t.claimed_at && t.state === "sending" && !t.sent_at && !t.started_at;
- const sentBack = why === "update-worker" ? "its graph is another on the worker's version (it waits for the worker's update)" : "more than its PC runs";
+ const sentBack = why === "update-worker" ? "its graph is another on the worker's version (it waits for the worker's update)" : why === "out-of-memory" ? GIVE_BACK_WORDS[why] : "more than its PC runs";
if (!backInLine(t, untouched ? { blame: false, sentBack } : { blame: true, why: GAVE_BACK_WORDS, sentBack })) return;
if (taskById(t.id)?.state !== "waiting") return;
setTask(t.id, { extra_json: JSON.stringify(extra), ...(untouched && { claimed_at: typeof claimedBefore === "number" ? claimedBefore : null }) });
@@ -1529,6 +1530,7 @@ const GIVE_BACK_WORDS: Record<string, string> = {
"run-limit": "it ran past its run limit",
media: "the worker can't make its clips or sound again",
"self-check": "the worker's check before the run didn't pass",
+ "out-of-memory": "it ran out of memory on that worker (another worker takes it)",
};
// ---------------------------------------------------------------------------
diff --git a/tests/worker-runner.test.ts b/tests/worker-runner.test.ts
index f3aba6df..01fd91a7 100644
--- a/tests/worker-runner.test.ts
+++ b/tests/worker-runner.test.ts
@@ -234,6 +234,20 @@ describe("a worker's runner", () => {
expect(state(id2).state).toBe("failed");
});
+ it("out of memory, then its retry doesn't move: given back as out of memory within minutes, never offered to this worker again", async () => {
+ const w = worker();
+ const id = task();
+ const s = studio(w);
+ fake.opts.failNext = "oom";
+ // The retry hangs; the PC's stall limit is an hour, so only the shorter limit for a retry after running out of memory ends it.
+ await runner(s, hooks({ stallLocalMs: 60 * 60_000, free: async () => void (fake.opts.failNext = "never") }), fastClock(600)).claimAndRun();
+ expect(s.reports.at(-1)).toEqual({ event: "give-back", why: "out-of-memory" });
+ expect(state(id)).toMatchObject({ state: "waiting", requeues: 1, worker_id: null });
+ expect(JSON.parse(String(state(id).extra_json)).refusedBy?.[0]).toMatch(new RegExp(`^${w.id}:`));
+ const q = (await (await realFetch(`${fake.url}/queue`)).json()) as { queue_running: unknown[]; queue_pending: unknown[] };
+ expect([...q.queue_running, ...q.queue_pending]).toEqual([]);
+ });
+
it("a job that stalls goes back to the line within the stall limit, with its prompt taken off ComfyUI", async () => {
const w = worker();
const id = task();