Jump to solution
Verify

The Fix

Upgrade to version 0.23.0 or later.

Based on closed encode/httpx issue #2139 · PR/commit linked

Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.

Jump to Verify Open PR/Commit
@@ -900,7 +900,7 @@ def send( return response - except Exception as exc: + except BaseException as exc: response.close()
repro.py
import asyncio import httpx async def main(): url = "https://httpbin.org/drip?delay=0&duration=5" # Or use local httpbin server: # docker run -ti -p 8088:80 kennethreitz/httpbin:latest url = "http://127.0.0.1:8088/drip?delay=0&duration=5" async with httpx.AsyncClient(timeout=10, trust_env=False) as client: try: coro = client.get(url) response = await asyncio.wait_for(coro, 3) except Exception as ex: print(type(ex), repr(ex)) else: print(response) if __name__ == "__main__": asyncio.run(main())
verify
Re-run: python ./example.py
fix.md
Option A — Upgrade to fixed release\nUpgrade to version 0.23.0 or later.\nWhen NOT to use: This fix is not applicable if the code does not handle cancellations properly.\n\n

Why This Fix Works in Production

  • Trigger: <class 'asyncio.exceptions.TimeoutError'> TimeoutError()
  • Mechanism: Closes the issue by ensuring that responses are closed when cancellations occur during reading, addressing the RuntimeError caused by unhandled exceptions.
  • Why the fix works: Closes the issue by ensuring that responses are closed when cancellations occur during reading, addressing the RuntimeError caused by unhandled exceptions. (first fixed release: 0.23.0).
Production impact:
  • If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.

Why This Breaks in Prod

  • Shows up under Python 3.9 in real deployments (not just unit tests).
  • Surfaces as: <class 'asyncio.exceptions.TimeoutError'> TimeoutError()

Proof / Evidence

  • GitHub issue: #2139
  • Fix PR: https://github.com/encode/httpx/pull/2156
  • First fixed release: 0.23.0
  • Reproduced locally: No (not executed)
  • Last verified: 2026-02-08
  • Confidence: 0.75
  • Did this fix it?: Yes (upstream fix exists)
  • Own content ratio: 0.25

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“> I'd suggest as a next step we find a reproduction script that only uses HTTPCore, see async usage here: https://www.encode.io/httpcore/async/”
@lovelydinosaur · 2022-03-28 · confirmation · source
“Hi all, This seems like something that's affecting several people so let's catch on with this”
@florimondmanca · 2022-03-28 · confirmation · source
“Hi! I had just investigated an issue which is probably the same bug”
@remi-dupre · 2022-03-22 · repro detail · source
“Hi @florimondmanca, I submit a PR https://github.com/encode/httpx/pull/2148 to resolve issue, could you help review? The PR not fix case of @remi-dupre, which seems another issue…”
@guyskk · 2022-03-28 · source

Failure Signature (Search String)

  • <class 'asyncio.exceptions.TimeoutError'> TimeoutError()

Error Message

Stack trace
error.txt
Error Message ------------- <class 'asyncio.exceptions.TimeoutError'> TimeoutError() Traceback (most recent call last): File "/Users/kk/dev/zhuwen/httpx/http_error2.py", line 22, in <module> asyncio.run(main()) File "/Users/kk/.pyenv/versions/3.9.7/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "/Users/kk/.pyenv/versions/3.9.7/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete return future.result() File "/Users/kk/dev/zhuwen/httpx/http_error2.py", line 18, in main print(response) File "/Users/kk/dev/zhuwen/httpx/httpx/_client.py", line 1978, in __aexit__ await self._transport.__aexit__(exc_type, exc_value, traceback) File "/Users/kk/dev/zhuwen/httpx/httpx/_transports/default.py", line 332, in __aexit__ await self._pool.__aexit__(exc_type, exc_value, traceback) File "/Users/kk/.pyenv/versions/3.9.7/envs/httpx/lib/python3.9/site-packages/httpcore/_async/connection_pool.py", line 326, in __aexit__ await self.aclose() File "/Users/kk/.pyenv/versions/3.9.7/envs/httpx/lib/python3.9/site-packages/httpcore/_async/connection_pool.py", line 312, in aclose raise RuntimeError( RuntimeError: The connection pool was closed while 1 HTTP requests/responses were still in-flight.
Stack trace
error.txt
Error Message ------------- $ python ./example.py <class 'asyncio.exceptions.TimeoutError'> TimeoutError() Traceback (most recent call last): File "/Users/tomchristie/Temp/./example.py", line 34, in <module> asyncio.run(main()) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete return future.result() File "/Users/tomchristie/Temp/./example.py", line 31, in main print(response) File "/Users/tomchristie/Temp/venv/lib/python3.9/site-packages/httpx/_client.py", line 1977, in __aexit__ await self._transport.__aexit__(exc_type, exc_value, traceback) File "/Users/tomchristie/Temp/venv/lib/python3.9/site-packages/httpx/_transports/default.py", line 332, in __aexit__ await self._pool.__aexit__(exc_type, exc_value, traceback) File "/Users/tomchristie/Temp/venv/lib/python3.9/site-packages/httpcore/_async/connection_pool.py", line 326, in __aexit__ await self.aclose() File "/Users/tomchristie/Temp/venv/lib/python3.9/site-packages/httpcore/_async/connection_pool.py", line 312, in aclose raise RuntimeError( RuntimeError: The connection pool was closed while 1 HTTP requests/responses were still in-flight.

Minimal Reproduction

repro.py
import asyncio import httpx async def main(): url = "https://httpbin.org/drip?delay=0&duration=5" # Or use local httpbin server: # docker run -ti -p 8088:80 kennethreitz/httpbin:latest url = "http://127.0.0.1:8088/drip?delay=0&duration=5" async with httpx.AsyncClient(timeout=10, trust_env=False) as client: try: coro = client.get(url) response = await asyncio.wait_for(coro, 3) except Exception as ex: print(type(ex), repr(ex)) else: print(response) if __name__ == "__main__": asyncio.run(main())

Environment

  • Python: 3.9
  • httpx: 0.20

What Broke

RuntimeError occurs when the connection pool is closed while requests are still in-flight.

Fix Options (Details)

Option A — Upgrade to fixed release Safe default (recommended)

Upgrade to version 0.23.0 or later.

When NOT to use: This fix is not applicable if the code does not handle cancellations properly.

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/2156

First fixed release: 0.23.0

Last verified: 2026-02-08. Validate in your environment.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix is not applicable if the code does not handle cancellations properly.

Verify Fix

verify
Re-run: python ./example.py

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.
  • Track RSS + object counts after deployments; alert on monotonic growth and GC pressure.
  • Add a long-running test that repeats the failing call path and asserts stable memory.

Version Compatibility Table

VersionStatus
0.23.0 Fixed

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.