httpx icon indicating copy to clipboard operation
httpx copied to clipboard

Fix client response stalling, exception handling, and disconnects in ASGI Transport

Open AviNoah opened this issue 1 year ago • 5 comments
trafficstars

ASGI transport layer would stall indefinitely due to improper stop signaling of the send stream, now close them when client closes response or stream is exhausted. It also would not raise exceptions correctly from run_app.

This happens because the receive stream expects to receive more values after send stream stopped producing values, and the async for loop stalls - leading the client to wait for the response content indefinitely. To amend this, we now close the send stream after more body is False.

Whenever the response is closed, cancel the run_app task if still running, and aclose the send and receive memory streams.

in the case of an exception from run_app, raise it, and then also aclose the send and receive memory streams.

Added tests for exhausting the stream, interrupting it and disconnecting the response, and interrupting it without disconnecting the response and continuing to consume it

Checklist

  • [X] I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • [X] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • [X] I've updated the documentation accordingly.

Fixes #2186

AviNoah avatar Oct 02 '24 15:10 AviNoah