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 text slot: an answer that meets a dropped connection is sent again; a cut wait is not a stopped job

Adarsh53416fd9aed9
diff --git a/bin/lib/worker/text.mjs b/bin/lib/worker/text.mjs
index cb8e3c98..7e2c33b9 100644
--- a/bin/lib/worker/text.mjs
+++ b/bin/lib/worker/text.mjs
@@ -11,6 +11,10 @@ import fs from "node:fs";
import path from "node:path";
import { comfyEnv, killTree, pickPort, RotatingLog } from "./comfy.mjs";
+/** How many times an answer goes to the studio before it is given up (1 s, 2 s, 4 s apart). */
+export const ANSWER_TRIES = 4;
+/** A cut wait for a text job is said in the log at most this often. */
+export const WAIT_CUT_SAY_MS = 10 * 60_000;
export const TEXT_DEFAULTS = { context: 16_384, idleMin: 10, gpuMinFreeMb: 8000, cpu: true, threads: 8, startMs: 180_000, answerMs: 120_000, claimWaitMs: 20_000 };
/** A job that sends its words as they come: how often it sends them, how often it says it is alive with none, its whole time. */
@@ -330,6 +334,7 @@ export class TextSlot {
this.job = Promise.resolve();
this.lastJobAt = 0;
this.missedAt = -Infinity;
+ this.waitCutSaidAt = -Infinity;
// Ends a claim that waits for a job when the worker stops.
this.ending = new AbortController();
// The claim that waits now (ended when the slot stops taking jobs), and whether a job is in hand.
@@ -413,7 +418,16 @@ export class TextSlot {
.then((had) => (this.missedAt = had ? -Infinity : this.now()))
.catch((e) => {
this.missedAt = this.now();
- if (!this.ending.signal.aborted) this.log(`A text job stopped: ${e.message}`);
+ if (this.ending.signal.aborted) return;
+ // A cut wait took no job: said once in a while, not for each blip.
+ if (e?.waitCut) {
+ if (this.now() - this.waitCutSaidAt >= WAIT_CUT_SAY_MS) {
+ this.waitCutSaidAt = this.now();
+ this.log(`The text slot's ${e.message}.`);
+ }
+ return;
+ }
+ this.log(`A text job stopped: ${e.message}`);
})
.finally(() => (this.busy = false));
}
@@ -437,6 +451,9 @@ export class TextSlot {
} catch (e) {
// The slot stopped taking jobs while it waited for one: no job, nothing went wrong.
if (claiming.signal.aborted && !this.ending.signal.aborted) return false;
+ // The wait itself was cut (the site restarted, the network dropped for a moment): no job was taken, so nothing is
+ // lost; it asks again after a few seconds.
+ if (!this.ending.signal.aborted) throw Object.assign(new Error(`the wait for a text job was cut (${e?.message ?? e}): no job was taken, it asks again`), { waitCut: true });
throw e;
} finally {
if (this.claiming === claiming) this.claiming = null;
@@ -467,11 +484,34 @@ export class TextSlot {
answer = { error: (this.d.redact ? this.d.redact(why) : why).slice(0, 200) };
}
this.lastJobAt = this.now();
- const sent = await this.d.call(`/api/gpu/computer/text/${encodeURIComponent(job.id)}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ lease: job.lease, ...answer }) });
- await sent.body?.cancel().catch(() => {});
+ await this.sendAnswer(job, answer);
return true;
}
+ /**
+ * The answer to the studio, with its lease. A dropped connection (the site restarting for a deploy, this PC's network
+ * for a moment) or a busy or restarting studio (429, 5xx) is tried again, ANSWER_TRIES times in all, so a person's
+ * answer isn't lost to a blip; any other answer (the job ended, the lease is someone else's) isn't.
+ */
+ async sendAnswer(job, answer) {
+ const sleep = this.d.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
+ const body = JSON.stringify({ lease: job.lease, ...answer });
+ for (let i = 1; ; i++) {
+ let res = null;
+ let err = null;
+ try {
+ res = await this.d.call(`/api/gpu/computer/text/${encodeURIComponent(job.id)}`, { method: "POST", headers: { "Content-Type": "application/json" }, body });
+ } catch (e) {
+ err = e;
+ }
+ await res?.body?.cancel().catch(() => {});
+ const again = err !== null || res.status === 429 || res.status >= 500;
+ if (!again) return;
+ if (i >= ANSWER_TRIES) throw new Error(`its answer to text job ${job.id} didn't reach the studio after ${ANSWER_TRIES} tries (${err ? err.message : `HTTP ${res.status}`})`);
+ await sleep(1000 * 2 ** (i - 1));
+ }
+ }
+
/** The answer's words from its server. */
async complete(job) {
const s = this.server;
diff --git a/tests/worker-text-slot.test.ts b/tests/worker-text-slot.test.ts
index 2e8fba90..3218c485 100644
--- a/tests/worker-text-slot.test.ts
+++ b/tests/worker-text-slot.test.ts
@@ -267,6 +267,41 @@ describe("the text slot", () => {
expect(s.own().mb).toBe(3698);
});
+ it("an answer that meets a dropped connection or a restarting studio is sent again, and arrives", async () => {
+ const site = studio([{ id: "hlp_r", lease: "LR", user: "Write a chorus", maxTokens: 200 }]);
+ let tries = 0;
+ const call = async (p: string, init?: RequestInit) => {
+ if (p === "/api/gpu/computer/text/hlp_r" && ++tries <= 2) {
+ if (tries === 1) throw new TypeError("fetch failed");
+ return new Response(null, { status: 503 });
+ }
+ return site.call(p, init);
+ };
+ const { s, logs } = makeSlot(call);
+ await until(s, Date.now, { may: true, room: "gpu" }, () => site.answers.length === 1);
+ expect(tries).toBe(3);
+ expect(site.answers[0]).toMatchObject({ id: "hlp_r", body: { lease: "LR" } });
+ expect(logs.some((l) => l.includes("A text job stopped"))).toBe(false);
+ });
+
+ it("a cut wait for a job took none: said once as that, never as a stopped job", async () => {
+ let cuts = 0;
+ const call = async (p: string) => {
+ if (p === "/api/gpu/computer/text/claim") {
+ cuts++;
+ throw new TypeError("fetch failed");
+ }
+ return new Response(null, { status: 404 });
+ };
+ const clock = { t: Date.now() };
+ const { s, logs } = makeSlot(call, { clock });
+ await until(s, () => (clock.t += 5000), { may: true, room: "gpu" }, () => cuts >= 3);
+ const said = logs.filter((l) => l.includes("wait for a text job was cut"));
+ expect(said).toHaveLength(1);
+ expect(said[0]).toContain("no job was taken");
+ expect(logs.some((l) => l.includes("A text job stopped"))).toBe(false);
+ });
+
it("a GPU that isn't free: the next start runs on the CPU only, and an idle server stops", async () => {
const clock = { t: Date.now() };
const site = studio([