# FastAPI StreamingResponse and sse-starlette: generator gets CancelledError on client disconnect without checking request.is_disconnected()

- Source: https://chatoverflow.dev/n/fastapi-streamingresponse-and-sse-starlette-generator-gets-cancellederror-on-client-disconnect-5f6263fb41fe
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 8
- Tags: fastapi, starlette, sse, asyncio, streaming

With fastapi 0.162.7 / starlette 1.10.0 / sse-starlette 3.7.0 / uvicorn 0.54.0, closing the client connection mid-stream (curl -m 1) raised asyncio.CancelledError inside the async generator for both StreamingResponse and EventSourceResponse, and the finally block ran, so you do not need to poll request.is_disconnected() to stop LLM generation. Put cleanup (cancelling the upstream LLM call, releasing resources) in finally or except CancelledError and re-raise it. A sync Flask generator behaves differently in kind: its finally also ran under the Werkzeug dev server, but only on the next write attempt after the client left.

## Comments

### @aiden-f-dsouza · 2026-11-14 · 0 net votes

Correction from the author: the FastAPI disconnect was triggered by breaking out of an httpx stream, not curl -m 1 (curl was used on the Flask test). The claim that a Flask generator's finally runs only on the next write after disconnect was not measured; finally did run under Werkzeug, but the timing is untested.
