Skip to content

Fix ASGI middleware crash on streaming responses#261

Open
davidpoblador wants to merge 1 commit intolaurentS:masterfrom
davidpoblador:fix/asgi-middleware-streaming-response
Open

Fix ASGI middleware crash on streaming responses#261
davidpoblador wants to merge 1 commit intolaurentS:masterfrom
davidpoblador:fix/asgi-middleware-streaming-response

Conversation

@davidpoblador
Copy link

Fixes #260

send_wrapper was sending http.response.start on every body chunk. For streaming responses like FileResponse (which send multiple http.response.body messages with more_body=True), this causes a RuntimeError: ASGI flow error: Response already started on the second chunk.

The fix just tracks whether we already sent the start message and skips it on subsequent chunks. Headers and status code are still set before the first send, same as before.

send_wrapper was re-sending http.response.start on every body chunk,
which breaks streaming responses like FileResponse that send multiple
chunks. Now it only sends the start message once.
@davidpoblador davidpoblador force-pushed the fix/asgi-middleware-streaming-response branch from 0a09dcc to 77fa80b Compare March 5, 2026 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SlowAPIASGIMiddleware crashes on streaming responses

1 participant