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: a stop that comes after the result was saved leaves the run saved
Adarsh1555d35adc6e
diff --git a/src/server/generations.ts b/src/server/generations.tsindex a653005b..76019eaf 100644--- a/src/server/generations.ts+++ b/src/server/generations.ts@@ -3161,7 +3161,7 @@ export async function cancelGeneration(id: string): Promise<void> {// awaits, so it can't mark the job done in between.if ((JSON.parse(r.external_meta_json ?? "{}") as Record<string, unknown>).finishing) {stopModelFinish(id);- update(id, { status: "canceled", completed_at: Date.now(), message: null });+ markCanceled(id);return;}const model = getModel(r.model_id);@@ -3182,11 +3182,23 @@ export async function cancelGeneration(id: string): Promise<void> {else if ("run" in own) await own.run(cancel);}stopModelFinish(id);- update(id, { status: "canceled", completed_at: Date.now(), message: null });+ // Saved meanwhile (here, or by another app process while the provider was asked to stop): it stays as it is.+ if (!markCanceled(id)) return;// Steps waiting only to feed this one are dropped too (nothing was spent on them).dropUnused(depIds(r.depends_on));}+/**+ * End a run as canceled, only while it still goes (queued or running: a run held for its output check too). A run that+ * was saved, failed or canceled since the cancel looked at it stays as it is, with its result and its report. A save+ * still under way doesn't stop it: the cancel wins, and that save ends with nothing kept (updateLive). Returns whether+ * the run was canceled.+ */+function markCanceled(id: string): boolean {+ const now = Date.now();+ return db().prepare("UPDATE generations SET status = 'canceled', completed_at = ?, message = NULL, updated_at = ? WHERE id = ? AND status IN ('queued', 'running')").run(now, now, id).changes > 0;+}+// ---------------------------------------------------------------------------// Fuobe Soul's post steps (the look's grade and overlay), made in the browser when the result arrives.diff --git a/tests/model3d-runner.test.ts b/tests/model3d-runner.test.tsindex 1df68bab..3497620e 100644--- a/tests/model3d-runner.test.ts+++ b/tests/model3d-runner.test.ts@@ -18,6 +18,8 @@ type Store = { read: () => OutputFile[] | null; write: (o: OutputFile[]) => voidconst finishes = new Map<string, { outputs: OutputFile[]; done: Done }>();const turntables = new Map<string, Store>();const stopped: string[] = [];+/** What happens elsewhere at the moment a job's finish is told to stop (another app process finishing it). */+const onStop = new Map<string, () => void>();vi.mock("@/server/model3d-finish", async (importOriginal) => {const real = await importOriginal<typeof import("@/server/model3d-finish")>();return {@@ -26,7 +28,10 @@ vi.mock("@/server/model3d-finish", async (importOriginal) => {if (!finishes.has(id)) finishes.set(id, { outputs, done: o.done });},startTurntable: (id: string, store: Store) => void turntables.set(id, store),- stopModelFinish: (id: string) => void stopped.push(id),+ stopModelFinish: (id: string) => {+ stopped.push(id);+ onStop.get(id)?.();+ },};});@@ -165,4 +170,15 @@ describe("the runner and the 3D finish", () => {expect(gens.getGeneration("job-cancel")!.status).toBe("canceled");expect(turntables.has("job-cancel")).toBe(false);});++ it("a cancel that comes as the finish ends (in another app process) leaves the finished model as it is", async () => {+ insertJob("job-done-meanwhile");+ gens.ensureRunner();+ const started = await until(() => finishes.get("job-done-meanwhile"));+ // The finish ends between the cancel's look at the job and its change of it.+ onStop.set("job-done-meanwhile", () => started.done(started.outputs));+ await gens.cancelGeneration("job-done-meanwhile");+ expect(gens.getGeneration("job-done-meanwhile")!.status).toBe("succeeded");+ expect(turntables.has("job-done-meanwhile")).toBe(true);+ });});diff --git a/tests/recovery-server.test.ts b/tests/recovery-server.test.tsindex b5d60735..0aeaa1f3 100644--- a/tests/recovery-server.test.ts+++ b/tests/recovery-server.test.ts@@ -58,7 +58,7 @@ 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 { getGeneration, ensureRunner, QUIET_MS, updateLive } = await import("@/server/generations");+const { cancelGeneration, getGeneration, ensureRunner, QUIET_MS, updateLive } = await import("@/server/generations");const pool = await import("@/server/computer/pool");const tasks = await import("@/server/computer/tasks");const billing = await import("@/server/billing");@@ -1743,6 +1743,47 @@ describe("two app processes on one database (a deploy's overlap)", () => {await expectOnce(id, watch, { prompts: 1, place: 1 });});+ it("a stop that lands after another process saved the result, while it waited for the workers, leaves the run saved: its file stays, and it settles once", async () => {+ const w = standInWorker();+ const { id } = await pressRun();+ const watch = watchOf(id, { "other process": there(id) });+ const d = driver(w);+ d.onStep(async (step) => {+ if (step === "part") return;+ await tick();+ await watch.look();+ });+ await d.beat();+ expect(await d.runOne()).toBe(true);+ expect(getGeneration(id)?.status).toBe("running");+ // The stop (a press of Stop in another tab) asks the run's workers to drop it, and waits for their answer.+ let asked!: () => void;+ const reached = new Promise<void>((r) => (asked = r));+ let answer!: () => void;+ const answered = new Promise<void>((r) => (answer = r));+ const realCancel = comfyProvider.cancel!;+ const cancel = vi.spyOn(comfyProvider, "cancel").mockImplementation(async (ctx) => {+ asked();+ await answered;+ return realCancel.call(comfyProvider, ctx);+ });+ try {+ const stopping = cancelGeneration(id);+ await reached;+ // Meanwhile the other process saves the result.+ await app2.tick();+ expect(getGeneration(id)?.status).toBe("succeeded");+ answer();+ await stopping;+ } finally {+ answer();+ cancel.mockRestore();+ }+ // The stop came too late: the run stays saved with its file, and its report is one settle, never a refund.+ expect(getGeneration(id)?.status).toBe("succeeded");+ await expectOnce(id, watch, { prompts: 1, place: 1 });+ });+it("a run stopped while its result is saved stays stopped, and the file saved for it goes (none is left filling the account's space)", async () => {const w = standInWorker();const { id } = await pressRun();