The Fix
Upgrade to version 0.21.2 or later.
Based on closed encode/httpx issue #838 · 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%.
@@ -38,6 +38,8 @@ async def __aiter__(self) -> AsyncIterator[bytes]:
class IteratorByteStream(SyncByteStream):
+ CHUNK_SIZE = 65_536
+
def __init__(self, stream: Iterable[bytes]):
import asyncio
import httpx
import aiohttp
URL = "https://example.com"
async def main() -> None:
for _ in range(0, 10_000):
async with httpx.AsyncClient() as client:
r = await client.get(URL)
print(r.status_code)
async def main2() -> None:
for _ in range(0, 10_000):
async with aiohttp.ClientSession() as session, session.get(URL) as r:
print(r.status)
if __name__ == '__main__':
asyncio.run(main2())
# 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.21.2 or later.\nWhen NOT to use: This fix is not applicable for clients that require immediate TLS setup for all requests.\n\n
Why This Fix Works in Production
- Trigger: Okay, so apparently one _massively useless_ thing we do is **eager TLS setup on `Client` instantiation**, _even though none of the eventually requested URLs…
- Mechanism: Eager TLS setup on Client instantiation slows down performance significantly
- Why the fix works: Improves file upload handling by using read(CHUNK_SIZE) instead of iter(), enhancing performance. (first fixed release: 0.21.2).
- 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
- Eager TLS setup on Client instantiation slows down performance significantly
- Production symptom (often without a traceback): Okay, so apparently one _massively useless_ thing we do is **eager TLS setup on `Client` instantiation**, _even though none of the eventually requested URLs uses HTTPS_.
Proof / Evidence
- GitHub issue: #838
- Fix PR: https://github.com/encode/httpx/pull/1948
- First fixed release: 0.21.2
- 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.57
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“> I wonder what could be the reasons for that I've not dug into the places you've linked, but a couple of things to note..”
“> optimizing for requests against an insecure host is probably marginally useful for real-world situations anyway Yep, an important one is a bunch of microservices…”
“> but I'm not sure how aiohttp handles defaults certs… Ah, so from ClientSession.request() I see that they use ssl.create_default_context(): > ssl: SSL validation mode”
“> what if loading the SSL context is needed in both aiohttp and httpx Yes, I actually just realized that optimizing for requests against an…”
Failure Signature (Search String)
- Okay, so apparently one _massively useless_ thing we do is **eager TLS setup on `Client` instantiation**, _even though none of the eventually requested URLs uses HTTPS_.
- => 92% (!) of the time spent instantiating the client (which itself is 62% of the total time making a single request with a client) is spent setting up SSL (`.load_ssl_context()`).
Copy-friendly signature
Failure Signature
-----------------
Okay, so apparently one _massively useless_ thing we do is **eager TLS setup on `Client` instantiation**, _even though none of the eventually requested URLs uses HTTPS_.
=> 92% (!) of the time spent instantiating the client (which itself is 62% of the total time making a single request with a client) is spent setting up SSL (`.load_ssl_context()`).
Error Message
Signature-only (no traceback captured)
Error Message
-------------
Okay, so apparently one _massively useless_ thing we do is **eager TLS setup on `Client` instantiation**, _even though none of the eventually requested URLs uses HTTPS_.
=> 92% (!) of the time spent instantiating the client (which itself is 62% of the total time making a single request with a client) is spent setting up SSL (`.load_ssl_context()`).
Minimal Reproduction
import asyncio
import httpx
import aiohttp
URL = "https://example.com"
async def main() -> None:
for _ in range(0, 10_000):
async with httpx.AsyncClient() as client:
r = await client.get(URL)
print(r.status_code)
async def main2() -> None:
for _ in range(0, 10_000):
async with aiohttp.ClientSession() as session, session.get(URL) as r:
print(r.status)
if __name__ == '__main__':
asyncio.run(main2())
# asyncio.run(main())
What Broke
HTTPX AsyncClient exhibits significantly slower performance compared to aiohttp under certain conditions.
Why It Broke
Eager TLS setup on Client instantiation slows down performance significantly
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
Upgrade to version 0.21.2 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/1948
First fixed release: 0.21.2
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix is not applicable for clients that require immediate TLS setup for all requests.
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.
- Add a TLS smoke test that performs a real handshake in CI (include CA bundle validation and hostname checks).
- Alert on handshake failures by error string and endpoint to catch cert/CA changes quickly.
Version Compatibility Table
| Version | Status |
|---|---|
| 0.21.2 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.