The Fix
Upgrade to version 0.13.4 or later.
Based on closed Kludex/starlette issue #1301 · PR/commit linked
@@ -10,7 +10,7 @@
from starlette.background import BackgroundTask
-from starlette.concurrency import iterate_in_threadpool
+from starlette.concurrency import iterate_in_threadpool, run_until_first_complete
from starlette.datastructures import URL, MutableHeaders
async def stream_file_response(request: Request) -> FileResponse:
bg = BackgroundTask(...)
return FileResponse(
path=PATH, filename="example.png", background=bg
)
###############################################################3
response = requests.get(
f"http://{HOST}:{PORT}/file",
stream=True,
)
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.13.4 or later.\nWhen NOT to use: This fix is not suitable for applications that do not require handling client disconnects.\n\n
Why This Fix Works in Production
- Trigger: The application doesn't start with the backgroundtask right away, but continues sending all the cunks first until finished.
- Mechanism: FileResponse does not listen for client disconnects, causing unnecessary streaming after disconnection
- Why the fix works: Adds a listener task to cancel streaming responses in case of an early disconnect, addressing the issue where FileResponse continues to send chunks even after the client disconnects. (first fixed release: 0.13.4).
- If left unfixed, this can cause silent data inconsistencies that propagate (bad cache entries, incorrect downstream decisions).
Why This Breaks in Prod
- Shows up under Python 3.9.7 in real deployments (not just unit tests).
- FileResponse does not listen for client disconnects, causing unnecessary streaming after disconnection
- Production symptom (often without a traceback): The application doesn't start with the backgroundtask right away, but continues sending all the cunks first until finished.
Proof / Evidence
- GitHub issue: #1301
- Fix PR: https://github.com/encode/starlette/pull/839
- First fixed release: 0.13.4
- 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.66
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“I'm not convinced we need to handle this”
“Sure, that's a reasonable decision”
“1”
Failure Signature (Search String)
- The application doesn't start with the backgroundtask right away, but continues sending all the cunks first until finished.
Copy-friendly signature
Failure Signature
-----------------
The application doesn't start with the backgroundtask right away, but continues sending all the cunks first until finished.
Error Message
Signature-only (no traceback captured)
Error Message
-------------
The application doesn't start with the backgroundtask right away, but continues sending all the cunks first until finished.
Minimal Reproduction
async def stream_file_response(request: Request) -> FileResponse:
bg = BackgroundTask(...)
return FileResponse(
path=PATH, filename="example.png", background=bg
)
###############################################################3
response = requests.get(
f"http://{HOST}:{PORT}/file",
stream=True,
)
response.close()
Environment
- Python: 3.9.7
What Broke
Clients experience wasted bandwidth and server resources due to continued streaming after disconnect.
Why It Broke
FileResponse does not listen for client disconnects, causing unnecessary streaming after disconnection
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
Upgrade to version 0.13.4 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/839
First fixed release: 0.13.4
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix is not suitable for applications that do not require handling client disconnects.
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
- Capture the exact failing error string in logs and tests so you can reproduce via a minimal script.
- Pin production dependencies and upgrade only with a reproducible test that hits the failing path.
Version Compatibility Table
| Version | Status |
|---|---|
| 0.13.4 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.