The Fix
Upgrade to version 0.21.0 or later.
Based on closed Kludex/starlette issue #1247 · 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%.
@@ -391,3 +391,34 @@ def test_streaming_response_known_size(test_client_factory):
response = client.get("/")
assert response.headers["content-length"] == "10"
+
+
[email protected]
import requests
def test_server():
response = requests.post('http://localhost:8000/', stream=True)
print('response.status', response.status)
response.close()
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.0 or later.\nWhen NOT to use: This fix is not applicable if using non-streaming responses or different testing frameworks.\n\n
Why This Fix Works in Production
- Trigger: In other words, remove anything that doesn't make the bug go away.
- Mechanism: Added a test to check that StreamingResponse will interrupt its streaming when the client disconnects.
- Why the fix works: Added a test to check that StreamingResponse will interrupt its streaming when the client disconnects. (first fixed release: 0.21.0).
- 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
- Production symptom (often without a traceback): In other words, remove anything that doesn't make the bug go away.
Proof / Evidence
- GitHub issue: #1247
- Fix PR: https://github.com/encode/starlette/pull/1727
- First fixed release: 0.21.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.65
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“We should take care of this for https://github.com/encode/starlette/issues/652 , I guess.”
“I think this will always be a limitation of the TestClient. I think we should add a test case, and maybe add a few lines…”
“A way to test an ASGI application with http.disconnect without the TestClient: https://github.com/encode/starlette/pull/1727”
Failure Signature (Search String)
- In other words, remove anything that doesn't make the bug go away.
- <!-- Any tracebacks, screenshots, etc. that can help understanding the problem.
Copy-friendly signature
Failure Signature
-----------------
In other words, remove anything that doesn't make the bug go away.
<!-- Any tracebacks, screenshots, etc. that can help understanding the problem.
Error Message
Signature-only (no traceback captured)
Error Message
-------------
In other words, remove anything that doesn't make the bug go away.
<!-- Any tracebacks, screenshots, etc. that can help understanding the problem.
Minimal Reproduction
import requests
def test_server():
response = requests.post('http://localhost:8000/', stream=True)
print('response.status', response.status)
response.close()
What Broke
Tests fail to receive disconnect events, leading to incomplete testing of streaming functionality.
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
Upgrade to version 0.21.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/starlette/pull/1727
First fixed release: 0.21.0
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix is not applicable if using non-streaming responses or different testing frameworks.
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 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.0 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.