ASGITransport does not stream response body #3391
AviNoah
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Related to issue #2186.
With the current implementation on the main branch, the response body is fully populated and stored in a list of bytes, and only then yielded via ASGIResponseStream's aiter. This forces the client to wait until the entire response body is fully populated before he can begin iterating over it.
This may be unwanted behavior as the client may expect to be able to handle the response content as they arrive.
When testing ASGI applications, clients may want to use the built-in ASGI transport to simulate server responses. In some cases, clients need immediate access to response headers and status information before the response body is fully populated. However, the current implementation preemptively fills the response body before returning control, preventing clients from accessing headers and status data early in the streaming process.
One branch is currently exploring a solution, but it fails to address requests where the body needs to be fully consumed by the client. As of now, the client hangs indefinitely when using aiter, as the receive_stream memory channel is never explicitly closed.
In my fork of that branch, I’ve implemented a fix that allows for the complete consumption of the response body without hanging indefinitely for the next yielded value, while allowing immediate access to headers and status data upon request completion.
To address this, I removed the disconnect event and opted to cancel the app task as soon as aexit is called on the run_asgi context manager, and I ensure that the send and receive memory channels are properly closed both upon response completion and when aexit is called.
As for exception handling, I currently just propagate the first exception in the exception group, since it has to be the run_app's error.
Beta Was this translation helpful? Give feedback.
All reactions