What this thread does
Do first: set demo.queue(max_size=20) and give every handler sharing one model the same concurrency_id with concurrency_limit=1.
Short answer
As of November 2026, the stable package shown by PyPI is Gradio 6.32.0. Stream by yielding partial outputs; keep conversation values in session state and share only the model resource. Queue limits bound admissions/execution, not per-user storage (queue guide).
Choose / don't choose
gr.State: choose for an independent history across calls within one page session; don't expect refresh persistence. Values must support a real deep copy (state guide).- Module-level state: choose for deliberately shared model resources; don't put mutable conversation lists or a user's current stream there.
- One shared concurrency group: choose for handlers competing for one model; don't put unrelated UI actions behind that model queue. Use a separate event without the shared ID for local UI changes.
Not for: model-server batch tuning; see vllm-sglang-llama-cpp-or-ollama-to-serve-a-team-llm-2026.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
Blocks.queue(max_size=10) | Documented bounded example; default None means unlimited events outside ZeroGPU Spaces, where it becomes 1. Select an application waiting budget | Current Blocks queue | Source |
concurrency_limit | 1 for the shared-model baseline; None means unlimited, not per-session isolation | Current events | Queue guide |
concurrency_id | Use one identical string for shared handlers; documented example "gpu_queue". The lowest configured limit in the group wins | Current events | Guide, events source |
default_concurrency_limit | 1 unless configured otherwise; explicit event limits override the default | Current Blocks queue | Source |
cancels | [click_event], where the value is the generation listener's returned event dependency | Current event API | Events source |
queue | False on the lightweight Stop/UI event so it can act without waiting for model generation | Current event API | Events source |
Order of operations
1. Create a gr.State([]) history and pass it as both event input and output. Inside the handler construct an updated session history rather than appending to a module-level list. Return/yield the state value along with the matching visible output (state guide). 2. Use a generator and yield a series of values matching the output components (streaming guide). Keep session history and the displayed response consistent at each yield; don't treat a cancelled partial answer as a completed one. 3. Bind Stop to the generation dependency with cancels=[click_event]; keep Stop outside the model concurrency group. Cancellation covers queued events and iterating generators, while an ordinary function already running is allowed to finish (event semantics). 4. Application lifecycle policy: put the upstream stream/producer in a context manager or try/finally; signal its cancellation, close the stream and release request-owned handles when iteration ends. A UI cancellation is not evidence that a background thread or remote generation stopped. Avoid releasing the shared model itself per request. Compare two independent browser sessions and cancel one mid-answer.
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
Queue needs to be enabled! | A cancel target has queuing disabled | Queue the generation event and call .queue() before .launch() | Blocks validation |
The data from one user overrides the data of the other user. | Reported state leak; the example's custom deep copy calls newone.__dict__.update(self.__dict__) | Use independent deep-copyable session values and inspect nested aliasing; do not read a shared object's initial .value as live session state | Historical report #9983, state contract |
Goudakrishna's dated rapid submit/cancel report #11497 additionally shows cancellation races; it does not establish a universal producer-cleanup recipe.
Missing
- Cleanup of the actual model backend is application-specific. These sources do not establish a fixed release for every cancellation race or guarantee interruption of running synchronous code.
Checked
2026-11-12; package registry, queue/state/streaming guides, Blocks/events source and two dated session/cancellation reports read. No demo or concurrent-session run executed.
Started from a post by @Gradio, packaged by the editor. Original post