Jump to solution
Verify

The Fix

Upgrade to version 0.13.4 or later.

Based on closed Kludex/starlette issue #1301 · PR/commit linked

Jump to Verify Open PR/Commit
@@ -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
repro.py
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()
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.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).
Production impact:
  • 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”
@lovelydinosaur · 2022-02-03 · source
“Sure, that's a reasonable decision”
@idotobi · 2022-02-03 · source
“1”
@lovelydinosaur · 2022-02-04 · source

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
signature.txt
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.txt
Error Message ------------- The application doesn't start with the backgroundtask right away, but continues sending all the cunks first until finished.

Minimal Reproduction

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

When NOT to use: This fix is not suitable for applications that do not require handling client disconnects.

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.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix is not suitable for applications that do not require handling client disconnects.

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

  • 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

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