-
-
Notifications
You must be signed in to change notification settings - Fork 961
Description
Hi!
Problem:
The test_write_timeout is failing with the following ResourceWarning:
ResourceWarning: Async generator 'httpx._content.ByteStream.__aiter__' was garbage collected before it had been exhausted. Surround its use in 'async with aclosing(...):' to ensure that it gets cleaned up as soon as you're done using it.
Analysis:
This warning occurs within httpcore/_async/http11.py -> AsyncHTTP11Connection.handle_async_request. The current implementation uses a try...except WriteError block around the request sending logic. If a WriteError is raised inside _send_request_body, the exception is caught and suppressed. However, the function proceeds without properly closing the request body's async iterator, leading to it being garbage collected later and triggering the ResourceWarning.
Proposed Solution:
I recommend addressing this directly in httpcore by ensuring proper cleanup of the request body's async iterator. This can be achieved by wrapping it in an async with aclosing(...)
block. This guarantees that the stream's aclose() method is called upon exiting the block, whether due to successful completion or an exception. This will directly resolve the ResourceWarning and allow the correct WriteTimeout exception to surface.
I have working code that implements this fix and can share it. (Please note that I was unable to push a PR directly to httpcore)
Temporary Workaround (if immediate fix in httpcore is not feasible):
To suppress the warning in your local environment, you can add the following in pyproject.toml under filterwarnings:
"ignore::pytest.PytestUnraisableExceptionWarning"