Jump to solution
Verify

The Fix

Upgrade to version 0.16.0 or later.

Based on closed Kludex/uvicorn issue #1230 · 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
@@ -1,3 +1,5 @@ @@ -1,3 +1,5 @@ +import asyncio + import httpx
repro.py
import asyncio from fastapi import FastAPI from starlette.websockets import WebSocket app = FastAPI() @app.websocket("/echo") async def echo_ws(ws: WebSocket): await ws.accept() await asyncio.sleep(1) data = await ws.receive_bytes() print(data)
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.16.0 or later.\nWhen NOT to use: Do not apply this fix if the application relies on the previous behavior of immediate disconnect handling.\n\n

Why This Fix Works in Production

  • Trigger: The call to `print(data)` should print `first`, and not raise a `starlette.websockets.WebSocketDisconnect` exception.
  • Mechanism: Fixes a bug where calling `WebSocketProtocol.asgi_receive` returned a close frame even if there were data messages in the read queue before the close frame.
  • Why the fix works: Fixes a bug where calling `WebSocketProtocol.asgi_receive` returned a close frame even if there were data messages in the read queue before the close frame. (first fixed release: 0.16.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

  • Shows up under Python 3.8.5 in real deployments (not just unit tests).
  • Production symptom (often without a traceback): The call to `print(data)` should print `first`, and not raise a `starlette.websockets.WebSocketDisconnect` exception.

Proof / Evidence

  • GitHub issue: #1230
  • Fix PR: https://github.com/kludex/uvicorn/pull/1252
  • First fixed release: 0.16.0
  • 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.68

Discussion

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

“@Kludex @florimondmanca @tomchristie @euri10 If we can revert the await self.ensure_open() change in asgi_receive without breaking other things in uvicorn, that would be an easy…”
@kylebebak · 2021-11-09 · source

Failure Signature (Search String)

  • The call to `print(data)` should print `first`, and not raise a `starlette.websockets.WebSocketDisconnect` exception.
  • This change was made in this commit: https://github.com/encode/uvicorn/commit/9a3040c9cd56844631b28631acd5862b5a4eafdd
Copy-friendly signature
signature.txt
Failure Signature ----------------- The call to `print(data)` should print `first`, and not raise a `starlette.websockets.WebSocketDisconnect` exception. This change was made in this commit: https://github.com/encode/uvicorn/commit/9a3040c9cd56844631b28631acd5862b5a4eafdd

Error Message

Signature-only (no traceback captured)
error.txt
Error Message ------------- The call to `print(data)` should print `first`, and not raise a `starlette.websockets.WebSocketDisconnect` exception. This change was made in this commit: https://github.com/encode/uvicorn/commit/9a3040c9cd56844631b28631acd5862b5a4eafdd

Minimal Reproduction

repro.py
import asyncio from fastapi import FastAPI from starlette.websockets import WebSocket app = FastAPI() @app.websocket("/echo") async def echo_ws(ws: WebSocket): await ws.accept() await asyncio.sleep(1) data = await ws.receive_bytes() print(data)

Environment

  • Python: 3.8.5

What Broke

Clients experienced unexpected disconnect exceptions when unread messages were present in the read queue.

Fix Options (Details)

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

Upgrade to version 0.16.0 or later.

When NOT to use: Do not apply this fix if the application relies on the previous behavior of immediate disconnect handling.

Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.

Fix reference: https://github.com/kludex/uvicorn/pull/1252

First fixed release: 0.16.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

  • Do not apply this fix if the application relies on the previous behavior of immediate disconnect handling.

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.16.0 Fixed

Related Issues

No related fixes found.

Sources

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