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%.
@@ -1,3 +1,5 @@
@@ -1,3 +1,5 @@
+import asyncio
+
import httpx
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)
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.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).
- 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…”
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
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 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
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.
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.
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
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.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.