Repository navigation
fix: avoid a second response start after a mid-stream ASGI error - #449
Open
rootkiller6788 wants to merge 2 commits into
Open
rootkiller6788 wants to merge 2 commits into
rootkiller6788 wants to merge 2 commits into
Conversation
When a streaming function raises after the first chunk, the status line is already on the wire. The exception middleware still wrote a 500 response start at that point, which is an invalid ASGI message sequence - uvicorn rejects it with "Expected ASGI message 'http.response.body', but got 'http.response.start'" and the real traceback gets buried in a second, duplicate one. Track whether the response has started and only write the crash response when nothing has been sent yet. The traceback is still logged either way.
Two cases for ExceptionHandlerMiddleware: a plain failure still produces the 500 crash response, and a failure after the response started sends nothing further. The second one regressed silently before - the client only noticed a truncated body.
rootkiller6788
marked this pull request as ready for review
December 3, 2026 04:11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A streaming HTTP function that raises after the first chunk lands in the ASGI exception middleware, which tries to write a 500 response start. The response had already started by then, so uvicorn rejects the message:
So you get the user's traceback, then the same traceback again buried under a protocol error, and a truncated response either way.
Fix is to remember whether
http.response.startwent out and skip the crash response when it did. The traceback is still logged first - we just don't send a response we can no longer send.Verified against a real uvicorn server with a streaming generator that raises on the second chunk: before, the log ends with the RuntimeError above; after, it ends with the user's ValueError plus uvicorn's "ASGI callable returned without completing response". Two tests cover both sides. Full suite 277 passed / 6 skipped, aio/init.py at 100% branch coverage, black + isort clean.