Skip to content

Images: first Generate after a model swap fails with 409 "Diffusion generation was cancelled." #8309

Closed

Description

@danielhanchen

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.

  1. /api/inference/images/status reports the swap inconsistently. A load-over-resident returned 200 with "loaded": true in the response body, and two seconds later GET /api/inference/images/status reported "loaded": false, "repo_id": null with every field null, while a generate issued at that moment succeeded. So during and just after a swap, status is not a reliable answer to "is a model ready". Sampling status every 250 ms across a swap, loaded never went false during the swap window and loading was never true, so a client has no way to know it should wait.

  2. 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 status does 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions