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.

Runner: a chain's next step is sent by one app process only

Adarsh616ff6839c71
diff --git a/src/server/db.ts b/src/server/db.ts
index 1755e99d..9d850ba9 100644
--- a/src/server/db.ts
+++ b/src/server/db.ts
@@ -1027,13 +1027,20 @@ const RUNNER_COLUMNS: [string, string, string][] = [
/**
* Each app process on this database, by the name its turns carry, and when it last said it still runs (generations.ts
- * keepBeating). A turn whose process went quiet is free before it runs out. Made here rather than as a numbered
+ * keepBeating). A turn whose process went quiet is free before it runs out. submit_turns: the app process handing a run
+ * to its provider right now (generations.ts takeSubmitTurn), so that two processes (a deploy's overlap) never both send
+ * a chain's next step, and neither fails a run the other is still sending. Made here rather than as a numbered
* migration, so branches that add migrations never clash over its place.
*/
const RUNNER_TABLES = `CREATE TABLE IF NOT EXISTS app_processes (
token TEXT PRIMARY KEY,
started_at INTEGER NOT NULL,
seen_at INTEGER NOT NULL
+ );
+ CREATE TABLE IF NOT EXISTS submit_turns (
+ id TEXT PRIMARY KEY,
+ held_by TEXT NOT NULL,
+ held_at INTEGER NOT NULL
);`;
/** Columns of the relay tables that came later: [table, column, type]. */
diff --git a/src/server/generations.ts b/src/server/generations.ts
index 4782984a..4a3f74dc 100644
--- a/src/server/generations.ts
+++ b/src/server/generations.ts
@@ -1541,12 +1541,19 @@ function chainMode(p: { demo: boolean; plan: { model: { name: string } }; route:
/** A waiting row's dependency ids as a json_each source (depends_on is one id, or a JSON list). */
const DEP_IDS = "json_each(CASE WHEN substr(g.depends_on, 1, 1) = '[' AND json_valid(g.depends_on) THEN g.depends_on ELSE json_array(g.depends_on) END) d";
+/**
+ * SQL: another app process that still runs is handing the run in `table` to its provider right now (takeSubmitTurn).
+ * Named parameters: @me (this process's name) and @quiet (since when a process that still runs has said so).
+ */
+const SENT_ELSEWHERE = (table: string) =>
+ `EXISTS (SELECT 1 FROM submit_turns s JOIN app_processes p ON p.token = s.held_by WHERE s.id = ${table}.id AND s.held_by != @me AND p.seen_at >= @quiet)`;
+
/**
* Waiting steps that can move: none of their dependencies is still queued or running (so they launch), or one
* failed, was canceled or is gone (so they fail now, not after the others finish). A per-frame render keeps
* hundreds of steps waiting for its lanes, and a plain "oldest 50 waiting" would hold every chain started after
* it until its backlog drained. The steps this process is handing to a provider right now are still "queued":
- * the limit makes room for them.
+ * the limit makes room for them. A step another app process is handing over is left to it.
*/
const READY_DEPENDENTS = `SELECT g.* FROM generations g
WHERE g.status = 'queued' AND g.external_id IS NULL AND g.depends_on IS NOT NULL
@@ -1554,7 +1561,8 @@ const READY_DEPENDENTS = `SELECT g.* FROM generations g
NOT EXISTS (SELECT 1 FROM ${DEP_IDS} JOIN generations dep ON dep.id = d.value WHERE dep.status IN ('queued', 'running'))
OR EXISTS (SELECT 1 FROM ${DEP_IDS} LEFT JOIN generations dep ON dep.id = d.value WHERE dep.id IS NULL OR dep.status IN ('failed', 'canceled'))
)
- ORDER BY g.created_at ASC LIMIT ?`;
+ AND NOT ${SENT_ELSEWHERE("g")}
+ ORDER BY g.created_at ASC LIMIT @limit`;
/** The error a waiting step gets when an input failed (framesProgress tells these from a step's own failure). */
export const waitedOnFailure = (frame?: boolean) => `${frame ? "The previous frame" : "An earlier step"} didn't finish`;
@@ -1621,7 +1629,9 @@ export const COMFY_WAIT = { ms: 60_000 };
/** Launch dependent jobs whose inputs finished; fail the ones whose inputs failed. Never waits for an upload. */
async function advanceDependents(): Promise<void> {
- const rows = db().prepare(READY_DEPENDENTS).all(50 + submitting().size) as Row[];
+ const rows = db()
+ .prepare(READY_DEPENDENTS)
+ .all({ limit: 50 + submitting().size, me: runnerToken(), quiet: Date.now() - QUIET_MS }) as Row[];
// A forced look at ComfyUI, at most one a sweep, for the steps that found it not answering.
let lookedAgain: Promise<boolean> | undefined;
for (const r of rows) {
@@ -1661,6 +1671,9 @@ async function advanceDependents(): Promise<void> {
fail(`${earlier} ${short}`);
continue;
}
+ // Every app process looks at the same waiting steps (a deploy runs two for a moment): only the one that takes the
+ // step's turn sends it, so it is sent once.
+ if (!takeSubmitTurn(r.id, { waiting: true })) continue;
submitting().add(r.id);
let handedOff = false;
// An account's step reads its inputs, and is planned, as its run was: as that account, on its own ComfyUI and its own
@@ -1746,7 +1759,7 @@ async function advanceDependents(): Promise<void> {
} catch (e) {
fail((e as Error).message);
} finally {
- if (!handedOff) submitting().delete(r.id);
+ if (!handedOff) handOverEnds(r.id);
}
}
}
@@ -1984,7 +1997,7 @@ function submit(id: string, route: ModelRoute, providerId: ProviderId, plan: Ret
// (A run is checked against the same table before it is made: hosted-run.ts stepsProblem.)
if (!poolRunsProvider(providerId)) {
update(id, { status: "failed", error: OWN_GPU_ONLY, completed_at: Date.now(), message: null });
- submitting().delete(id);
+ handOverEnds(id);
return Promise.resolve();
}
return runOnPool(req.account, () => withRunStamp(stampOf(req), () => submitNow(id, route, providerId, plan, req, index)));
@@ -1993,7 +2006,7 @@ function submit(id: string, route: ModelRoute, providerId: ProviderId, plan: Ret
if ("error" in own) {
update(id, { status: "failed", error: own.error, completed_at: Date.now(), message: null });
// A chained step advanceDependents marked as being handed over isn't any more.
- submitting().delete(id);
+ handOverEnds(id);
return Promise.resolve();
}
return own.run(() => submitNow(id, route, providerId, plan, req, index));
@@ -2010,6 +2023,11 @@ async function submitNow(
req: GenerationRequest,
index: number,
): Promise<void> {
+ // Another app process that still runs is handing this run over right now: it is left to that process.
+ if (!takeSubmitTurn(id)) {
+ submitting().delete(id);
+ return;
+ }
submitting().add(id);
// MiniMax H3's 2K hand-off, decided here, on the ComfyUI this run goes to.
if (providerId === "comfyui") {
@@ -2072,7 +2090,7 @@ async function submitNow(
return;
}
} finally {
- submitting().delete(id);
+ handOverEnds(id);
}
}
@@ -2237,9 +2255,12 @@ function resend(r: Row, account: string, words: string): boolean {
resentRuns().add(r.id);
if (resentRuns().size > 5000) resentRuns().delete(resentRuns().values().next().value!);
// (Only while the run still goes: one another app process saved or ended meanwhile is left as it is.)
+ // (It takes its turn to be sent in the same step: a chain's step waiting to be sent again is never sent by another
+ // app process too.)
const again = db().transaction((): boolean => {
if (!updateLive(r.id, { external_id: null, external_meta_json: null, status: "queued", progress: null, message: words })) return false;
db().prepare("DELETE FROM generation_runs WHERE id = ?").run(r.id);
+ noteSubmitTurn(r.id);
return true;
})();
if (!again) return true;
@@ -2727,11 +2748,18 @@ function startRunner(): void {
resumeTurntables();
}
-/** Jobs that never got an external id (the server died mid-submit) can't be recovered. Runs every tick. */
+/**
+ * Jobs that never got an external id (the server died mid-submit) can't be recovered. Runs every tick. One another app
+ * process that still runs is sending (a deploy's overlap: an account's files waiting their turn to go) is left to it.
+ */
function sweepInterrupted(): void {
+ const now = Date.now();
const rows = db()
- .prepare("SELECT id FROM generations WHERE status IN ('queued','running') AND external_id IS NULL AND depends_on IS NULL AND created_at < ?")
- .all(Date.now() - STALE_SUBMIT_MS) as Pick<Row, "id">[];
+ .prepare(
+ `SELECT g.id FROM generations g WHERE g.status IN ('queued','running') AND g.external_id IS NULL AND g.depends_on IS NULL AND g.created_at < @before
+ AND NOT ${SENT_ELSEWHERE("g")}`,
+ )
+ .all({ before: now - STALE_SUBMIT_MS, me: runnerToken(), quiet: now - QUIET_MS }) as Pick<Row, "id">[];
const busy = submitting();
for (const r of rows) {
if (busy.has(r.id)) continue;
@@ -3186,6 +3214,8 @@ function keepBeating(): void {
try {
beat(now);
db().prepare("DELETE FROM app_processes WHERE seen_at < ?").run(now - FORGET_MS);
+ // Turns to send a run that a process forgotten here held (it stopped part way): nobody holds them.
+ db().prepare("DELETE FROM submit_turns WHERE held_by NOT IN (SELECT token FROM app_processes)").run();
} catch (e) {
console.warn(`[generations] app process: ${(e as Error).message}`);
}
@@ -3208,6 +3238,7 @@ function giveTurnsBack(): void {
const me = runnerToken();
d.transaction(() => {
d.prepare("UPDATE generation_runs SET finish_by = NULL, finish_until = NULL WHERE finish_by = ? AND finish_until >= ?").run(me, Date.now());
+ d.prepare("DELETE FROM submit_turns WHERE held_by = ?").run(me);
d.prepare("DELETE FROM app_processes WHERE token = ?").run(me);
})();
} catch {
@@ -3243,6 +3274,53 @@ export function claimFinish(id: string, now = Date.now()): boolean {
.immediate();
}
+/**
+ * Take this app process's turn to hand a run to its provider: only when no other app process that still runs holds it
+ * (one quiet for 20 s, or gone, holds nothing any more). `waiting`: only while the run still waits to be sent (queued,
+ * with no job at its provider yet), as a chain's next step is, which every app process looks at (advanceDependents).
+ * Two processes on one database (a deploy's overlap) never both send a run, and neither fails one the other is still
+ * sending (sweepInterrupted). handOverEnds gives the turn back.
+ */
+function takeSubmitTurn(id: string, o: { waiting?: boolean } = {}, now = Date.now()): boolean {
+ keepBeating();
+ const d = db();
+ return d
+ .transaction((): boolean => {
+ if (o.waiting) {
+ const g = d.prepare("SELECT status, external_id FROM generations WHERE id = ?").get(id) as Pick<Row, "status" | "external_id"> | undefined;
+ if (!g || g.status !== "queued" || g.external_id) return false;
+ }
+ return (
+ d
+ .prepare(
+ `INSERT INTO submit_turns (id, held_by, held_at) VALUES (@id, @me, @now)
+ ON CONFLICT(id) DO UPDATE SET held_by = excluded.held_by, held_at = excluded.held_at
+ WHERE submit_turns.held_by = @me OR NOT EXISTS (SELECT 1 FROM app_processes p WHERE p.token = submit_turns.held_by AND p.seen_at >= @quiet)`,
+ )
+ .run({ id, me: runnerToken(), now, quiet: now - QUIET_MS }).changes > 0
+ );
+ })
+ .immediate();
+}
+
+/** This app process holds the turn to send a run (inside a step that made the run wait to be sent again). */
+function noteSubmitTurn(id: string): void {
+ keepBeating();
+ db()
+ .prepare("INSERT INTO submit_turns (id, held_by, held_at) VALUES (?, ?, ?) ON CONFLICT(id) DO UPDATE SET held_by = excluded.held_by, held_at = excluded.held_at")
+ .run(id, runnerToken(), Date.now());
+}
+
+/** This app process has sent the run, or stopped trying: it is no longer being handed over here, and its turn goes. */
+function handOverEnds(id: string): void {
+ submitting().delete(id);
+ try {
+ db().prepare("DELETE FROM submit_turns WHERE id = ? AND held_by = ?").run(id, runnerToken());
+ } catch {
+ // A closed database: the turn is free once this process has been quiet for 20 s.
+ }
+}
+
/** Start (or keep running) the 3D finish of a job whose outputs are stored; it marks the job done when it ends. */
function resumeFinish(r: Row): void {
const outputs = JSON.parse(r.outputs_json || "[]") as OutputFile[];
diff --git a/tests/recovery-server.test.ts b/tests/recovery-server.test.ts
index 7b9a184b..0ebc4c43 100644
--- a/tests/recovery-server.test.ts
+++ b/tests/recovery-server.test.ts
@@ -58,7 +58,9 @@ 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 { cancelGeneration, getGeneration, ensureRunner, QUIET_MS, updateLive } = await import("@/server/generations");
+const { cancelGeneration, createGenerations, getGeneration, ensureRunner, QUIET_MS, updateLive } = await import("@/server/generations");
+const { runOnPool } = await import("@/server/hosted");
+const { stampOf, withRunStamp } = await import("@/server/run-stamp");
const pool = await import("@/server/computer/pool");
const { catalogVersion } = await import("@/server/computer/catalog-version");
const tasks = await import("@/server/computer/tasks");
@@ -1591,6 +1593,33 @@ describe("two app processes on one database (a deploy's overlap)", () => {
await expect(app2.generation(id, b1)).rejects.toThrow(/answered 403/);
});
+ it("a chain's next step both processes find ready at once is sent once: one job for it on the workers, run once", async () => {
+ const w = standInWorker();
+ const { id } = await pressRun();
+ const d = driver(w);
+ await workAll(d);
+ expect((await ended(id)).status).toBe("succeeded");
+ // The run's next step, made as its run's steps are (as the account, on this install's GPUs, with the run's stamp):
+ // its input, the first step's picture, is ready for both processes.
+ const first = JSON.parse((db().prepare("SELECT request_json FROM generations WHERE id = ?").get(id) as { request_json: string }).request_json);
+ const [step] = runOnPool(A, () => withRunStamp(stampOf(first), () => createGenerations({ feature: "image", modelId: "oss-selftest-image", op: "edit", prompt: "invert it", chainFrom: "first" }, { dependsOn: id })));
+ const sent = () => (db().prepare("SELECT external_id FROM generations WHERE id = ?").get(step.id) as { external_id: string | null }).external_id;
+ const jobs = () => db().prepare("SELECT id FROM relay_tasks WHERE generation_id = ?").all(step.id).length;
+ // Both runners turn at the same moment.
+ await Promise.all([tick(), app2.tick()]);
+ for (let i = 0; i < 200 && !sent(); i++) await sleep(25);
+ expect(sent(), "sent by the first turn of one of them").toBeTruthy();
+ // (A second hand-over, were there one, lands in the moments after.)
+ await sleep(1500);
+ await Promise.all([tick(), app2.tick()]);
+ expect(jobs(), "its jobs on the workers").toBe(1);
+ await workAll(d);
+ expect((await ended(step.id)).status).toBe("succeeded");
+ expect(jobs()).toBe(1);
+ const ran = db().prepare("SELECT COUNT(*) AS n FROM worker_jobs WHERE generation_id = ? AND event = 'finished'").get(step.id) as { n: number };
+ expect(ran.n, "the workers ran it once").toBe(1);
+ });
+
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();
diff --git a/tests/runtime-runner.test.ts b/tests/runtime-runner.test.ts
index 60083358..65422898 100644
--- a/tests/runtime-runner.test.ts
+++ b/tests/runtime-runner.test.ts
@@ -182,6 +182,55 @@ describe("restarts (S7)", () => {
});
});
+describe("two app processes on one database (a deploy's overlap)", () => {
+ /** Another app process, which said a moment ago (or `agoMs` ago) that it still runs. */
+ const otherApp = (token: string, agoMs = 0) =>
+ db().prepare("INSERT INTO app_processes (token, started_at, seen_at) VALUES (?, ?, ?) ON CONFLICT(token) DO UPDATE SET seen_at = excluded.seen_at").run(token, Date.now() - 60_000, Date.now() - agoMs);
+ /** That process is sending this run to its provider right now. */
+ const sendingThere = (id: string, token: string) => db().prepare("INSERT INTO submit_turns (id, held_by, held_at) VALUES (?, ?, ?) ON CONFLICT(id) DO UPDATE SET held_by = excluded.held_by").run(id, token, Date.now());
+ const turnOf = (id: string) => (db().prepare("SELECT held_by FROM submit_turns WHERE id = ?").get(id) as { held_by: string } | undefined)?.held_by ?? null;
+
+ it("a chain's next step the other process is sending is left to it; once that process goes quiet, this one sends it, once", async () => {
+ gens.ensureRunner();
+ const first = image("the step before");
+ expect((await settle(first.id)).status).toBe("succeeded");
+ otherApp("app-sending");
+ const [step] = gens.createGenerations({ feature: "t", modelId: "nano-banana-2", op: "edit", prompt: "Ink" }, { dependsOn: first.id });
+ // (Before this process's runner turns again: it looks at the step only with the next turn.)
+ sendingThere(step.id, "app-sending");
+ await sleep(3_500);
+ expect(row(step.id)).toMatchObject({ status: "queued", external_id: null });
+ // It was killed part way (20 s with no word from it): this process sends the step.
+ otherApp("app-sending", gens.QUIET_MS + 1000);
+ expect((await settle(step.id)).status).toBe("succeeded");
+ expect(turnOf(step.id), "the turn goes once it is sent").toBeNull();
+ });
+
+ it("a run the other process is still sending isn't failed as stopped before it started; one whose sender went quiet is", async () => {
+ gens.ensureRunner();
+ const insert = (id: string) =>
+ db()
+ .prepare(
+ `INSERT INTO generations (id, feature, model_id, engine, provider, op, kind, status, request_json, outputs_json, created_at, updated_at)
+ VALUES (?, 'image', 'nano-banana-pro', 'cloud', 'fal', 't2i', 'image', 'queued', '{"feature":"image","modelId":"nano-banana-pro"}', '[]', ?, ?)`,
+ )
+ .run(id, Date.now() - 5 * 60_000, Date.now() - 5 * 60_000);
+ otherApp("app-uploading");
+ otherApp("app-killed", gens.QUIET_MS + 1000);
+ insert("sent-elsewhere-1");
+ sendingThere("sent-elsewhere-1", "app-uploading");
+ insert("cut-off-2");
+ sendingThere("cut-off-2", "app-killed");
+ await until(() => row("cut-off-2").status === "failed", 5000);
+ expect(row("cut-off-2").error).toBe("This run stopped before it started. Press Retry.");
+ await sleep(1700);
+ expect(row("sent-elsewhere-1").status).toBe("queued");
+ // That process stops part way too: the run is failed at the next sweep.
+ otherApp("app-uploading", gens.QUIET_MS + 1000);
+ await until(() => row("sent-elsewhere-1").status === "failed", 5000);
+ });
+});
+
describe("cancel while submitting (S13)", () => {
it("cancels the provider's job when it lands, and keeps the row canceled", async () => {
const real = mockProvider.submit;