Jump to solution
Verify

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%.

Jump to Verify Open PR/Commit
@@ -391,3 +391,34 @@ def test_streaming_response_known_size(test_client_factory): response = client.get("/") assert response.headers["content-length"] == "10" + + [email protected]
repro.py
import requests def test_server(): response = requests.post('http://localhost:8000/', stream=True) print('response.status', response.status) response.close()
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
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).
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

  • 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.”
@Kludex · 2021-11-27 · source
“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…”
@Kludex · 2022-05-09 · source
“A way to test an ASGI application with http.disconnect without the TestClient: https://github.com/encode/starlette/pull/1727”
@Kludex · 2022-08-17 · source

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
signature.txt
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.txt
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

repro.py
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.

When NOT to use: This fix is not applicable if using non-streaming responses or different testing frameworks.

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.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix is not applicable if using non-streaming responses or different testing frameworks.

Verify Fix

verify
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

VersionStatus
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.