Repository navigation
Description
Switching diffusion models in the Images tab makes the next Generate fail with a 409 and the message "Diffusion generation was cancelled." Pressing Generate again works. Nothing was cancelled.
I can reproduce the user-visible behaviour reliably, and I have narrowed it to the teardown fence in DiffusionEngine, but I have not proven the exact leak, so I am filing this rather than guessing at a fix in code this concurrency-sensitive.
Reproduce
Against a running Studio, with unsloth/Z-Image-Turbo-GGUF:Q4_K_M:
A: unload -> load -> generate 200, 200 (cold load, fine)
B: load over the resident pipeline 409, 200 {"detail":"Diffusion generation was cancelled."}
C: load over the resident pipeline 409, 200 {"detail":"Diffusion generation was cancelled."}
The first generate after a load-over-resident always fails; the immediate retry always succeeds. Reproduced on two separate Studio instances.
In the UI this is: switch model, quant or LoRA in the Images tab, press Generate, get an error toast that says your generation was cancelled, press Generate again, it works.
Where it surfaces
studio/backend/core/inference/diffusion.py:
# generate(), ~line 4736
with self._generate_lock:
with self._lock:
# A teardown is waiting for this lock and Python locks are not FIFO, so refuse
# rather than start a denoise on a pipeline that is already being torn down.
if self._teardown_waiters:
raise RuntimeError(DIFFUSION_CANCELLED_MSG)_teardown_waiters is raised and lowered in two places, begin_load (~2598 / 2610) and unload (~5236 / 5249). Both lower it in a finally. The unload -> load -> generate path is clean, and only the load-over-resident path fails, so the fence appears to still be up when the generate arrives.
Two related observations from the same probe
These may be the same root cause or may be separate; both are reproducible.
-
/api/inference/images/statusreports the swap inconsistently. A load-over-resident returned200with"loaded": truein the response body, and two seconds laterGET /api/inference/images/statusreported"loaded": false, "repo_id": nullwith every field null, while a generate issued at that moment succeeded. So during and just after a swap,statusis not a reliable answer to "is a model ready". Samplingstatusevery 250 ms across a swap,loadednever went false during the swap window andloadingwas never true, so a client has no way to know it should wait. -
A load issued while a previous swap is still settling can itself return 409. In one run the load POST returned 409 after 0.16 s.
Why it matters beyond the retry
- The message blames the user for a cancellation that never happened, which is the worst kind of error text to show.
- 409 is not obviously retryable to a client; nothing in the response says "retry in a moment".
- Because
statusdoes not report the swap, neither the UI nor an API client can disable Generate or wait.
Suggested direction
Rather than refusing, a generation that arrives while a teardown fence is up could wait for the fence to drop (bounded), at which point it would block on _generate_lock for the rest of the load and then run, which is what the user expects from pressing Generate. If refusing is preferred, it should be a distinct, honest error ("a model load is in progress") with a retry hint, and status should report loading for the whole swap so the UI can gate the button.
Repro script
diffusion_409_repro.py, run against any Studio with that model available:
a.post("/api/inference/images/unload"); wait()
a.post("/api/inference/images/load", json=BODY); wait_loaded()
gen() # 200
a.post("/api/inference/images/load", json=BODY); wait_loaded()
gen() # 409 "Diffusion generation was cancelled."
gen() # 200
Activity