The Fix
Upgrade to version 0.13.0 or later.
Based on closed encode/httpx issue #634 · PR/commit linked
@@ -1,7 +1,6 @@
import functools
import ssl
-import sys
import typing
import asyncio
import httpx
async def main():
async with httpx.Client() as client:
response = await client.post(
"https://login.microsoftonline.com/common/oauth2/v2.0/token",
# You can keep those parameters, the issue happens also for 4XX responses
data={
"grant_type": "authorization_code",
"code": "code",
"redirect_uri": "http://localhost:8000/redirect",
"client_id": "CLIENT_ID",
"client_secret": "CLIENT_SECRET",
},
)
print(response.http_version) # HTTP/1.1
print(len(response.content)) # 485 (same as in Content-Length header)
print(response.headers)
# Never happens
print("Done")
return response
asyncio.run(main())
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Option A — Upgrade to fixed release\nUpgrade to version 0.13.0 or later.\nWhen NOT to use: This fix should not be applied if the application relies on waiting for the stream to close explicitly.\n\n
Why This Fix Works in Production
- Trigger: await self.close()
- Mechanism: The HTTPX client hangs during the closing process due to improper handling of the socket closure
- Why the fix works: Addresses the issue where the HTTPX client could hang during the closing process by avoiding the use of wait_closed(). (first fixed release: 0.13.0).
Why This Breaks in Prod
- Shows up under Python 3.7 in real deployments (not just unit tests).
- The HTTPX client hangs during the closing process due to improper handling of the socket closure
- Surfaces as: File "/Users/fvoron/.local/share/virtualenvs/httpx-oauth-5xgbpDkq/src/httpx/httpx/client.py", line 884, in __aexit__
Proof / Evidence
- GitHub issue: #634
- Fix PR: https://github.com/encode/httpx/pull/640
- First fixed release: 0.13.0
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.85
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.37
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Thank you for your feedback @florimondmanca :) I've tried what you suggest (although I have to admit my knowledge on the subject is quite limited):…”
“It seems fairly likely this might have been resolved by 0.9.4, just released. Some things to test out here... * Does the issue replicate with…”
“> Okay, thanks, that's _super_ helpful - what's the _absolute_ simplest case you can reduce this to? This: > I'm assuming the you wouldn't see…”
“Thank you @tomchristie! * Yes, the issue does replicate with 0.9.4. * No, the issue doesn't replicate with Trio. So, should we suspect a bug…”
Failure Signature (Search String)
- await self.close()
Error Message
Stack trace
Error Message
-------------
File "/Users/fvoron/.local/share/virtualenvs/httpx-oauth-5xgbpDkq/src/httpx/httpx/client.py", line 884, in __aexit__
await self.close()
File "/Users/fvoron/.local/share/virtualenvs/httpx-oauth-5xgbpDkq/src/httpx/httpx/client.py", line 873, in close
await self.dispatch.close()
File "/Users/fvoron/.local/share/virtualenvs/httpx-oauth-5xgbpDkq/src/httpx/httpx/dispatch/connection_pool.py", line 212, in close
await connection.close()
File "/Users/fvoron/.local/share/virtualenvs/httpx-oauth-5xgbpDkq/src/httpx/httpx/dispatch/connection.py", line 171, in close
await self.open_connection.close()
File "/Users/fvoron/.local/share/virtualenvs/httpx-oauth-5xgbpDkq/src/httpx/httpx/dispatch/http11.py", line 72, in close
await self.socket.close()
File "/Users/fvoron/.local/share/virtualenvs/httpx-oauth-5xgbpDkq/src/httpx/httpx/concurrency/asyncio.py", line 167, in close
await self.stream_writer.wait_closed()
File "/Users/fvoron/.pyenv/versions/3.7.5/lib/python3.7/asyncio/streams.py", line 323, in wait_closed
await self._protocol._closed
File "/Users/fvoron/.pyenv/versions/3.7.5/lib/python3.7/asyncio/selector_events.py", line 804, in _read_ready__data_received
data = self._sock.recv(self.max_size)
ConnectionResetError: [Errno 54] Connection reset by peer
Minimal Reproduction
import asyncio
import httpx
async def main():
async with httpx.Client() as client:
response = await client.post(
"https://login.microsoftonline.com/common/oauth2/v2.0/token",
# You can keep those parameters, the issue happens also for 4XX responses
data={
"grant_type": "authorization_code",
"code": "code",
"redirect_uri": "http://localhost:8000/redirect",
"client_id": "CLIENT_ID",
"client_secret": "CLIENT_SECRET",
},
)
print(response.http_version) # HTTP/1.1
print(len(response.content)) # 485 (same as in Content-Length header)
print(response.headers)
# Never happens
print("Done")
return response
asyncio.run(main())
Environment
- Python: 3.7
- httpx: 0.9.3
What Broke
Clients may experience hanging behavior during the closing of the HTTPX client, leading to unresponsive applications.
Why It Broke
The HTTPX client hangs during the closing process due to improper handling of the socket closure
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
Upgrade to version 0.13.0 or later.
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Fix reference: https://github.com/encode/httpx/pull/640
First fixed release: 0.13.0
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix should not be applied if the application relies on waiting for the stream to close explicitly.
Verify Fix
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Did This Fix Work in Your Case?
Quick signal helps us prioritize which fixes to verify and improve.
Prevention
- Add a CI check that diffs key outputs after upgrades (OpenAPI schema snapshots, JSON payload shapes, CLI output).
- Upgrade behind a canary and run integration tests against the canary before 100% rollout.
Version Compatibility Table
| Version | Status |
|---|---|
| 0.13.0 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.