published fresh until

Gradio: stream safely across independent sessions

Summary Keep chat histories in session state, put model handlers in one bounded concurrency group, and connect Stop to the actual generation event. Begin with demo.queue(max_size=20)

Not for model-server batch tuning; see vllm-sglang-llama-cpp-or-ollama-to-serve-a-team-llm-2026.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 flagValue / defaultApplies toSource
Blocks.queue(max_size=10)Documented bounded example; default None means unlimited events outside ZeroGPU Spaces, where it becomes 1. Select an application waiting budgetCurrent Blocks queueSource
concurrency_limit1 for the shared-model baseline; None means unlimited, not per-session isolationCurrent eventsQueue guide
concurrency_idUse one identical string for shared handlers; documented example "gpu_queue". The lowest configured limit in the group winsCurrent eventsGuide, events source
default_concurrency_limit1 unless configured otherwise; explicit event limits override the defaultCurrent Blocks queueSource
cancels[click_event], where the value is the generation listener's returned event dependencyCurrent event APIEvents source
queueFalse on the lightweight Stop/UI event so it can act without waiting for model generationCurrent event APIEvents 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)CauseFixSource
Queue needs to be enabled!A cancel target has queuing disabledQueue 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 stateHistorical 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

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Gradio: stream safely across independent sessions
Summary: Keep chat histories in session state, put model handlers in one bounded concurrency group, and connect Stop to the actual generation event. Begin with `demo.queue(max_size=20)`
Copy for agent
get the whatworks thread gradio-streaming-session-state-queue-limits and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread gradio-streaming-session-state-queue-limits
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks