The Fix
Upgrade to version 0.21.0 or later.
Based on closed Kludex/uvicorn issue #1344 · 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%.
@@ -44,7 +44,7 @@ plugins =
[coverage:report]
precision = 2
-fail_under = 98.50
+fail_under = 98.80
show_missing = true
class App:
def __init__(self, scope, receive, send):
self.scope = scope
self.receive = receive
self.send = send
def __await__(self):
return self.asgi().__await__()
async def asgi(self):
while True:
message = await self.receive()
message_type = message["type"].replace(".", "_")
print(message)
handler = getattr(self, message_type, None)
if handler is not None:
await handler(message)
if message_type == "websocket_disconnect":
break
async def websocket_connect(self, message):
await self.send({"type": "websocket.accept"})
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.21.0 or later.\nWhen NOT to use: This fix should not be applied if the server behavior is expected to remain unchanged.\n\n
Why This Fix Works in Production
- Trigger: Incorrect websocket close code on server shutdown
- Mechanism: The websocket close code 1006 is sent instead of the expected 1001 during server shutdown
- Why the fix works: Sends code 1012 on shutdown for websockets instead of the previous 1006, addressing the issue of incorrect websocket close codes during server shutdown. (first fixed release: 0.21.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.10.1 in real deployments (not just unit tests).
- The websocket close code 1006 is sent instead of the expected 1001 during server shutdown
- Production symptom (often without a traceback): Incorrect websocket close code on server shutdown
Proof / Evidence
- GitHub issue: #1344
- Fix PR: https://github.com/kludex/uvicorn/pull/1816
- First fixed release: 0.21.0
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.95
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.58
Verified Execution
We executed the runnable minimal repro in a temporary environment and captured exit codes + logs.
- Status: PASS
- Ran: 2026-02-11T16:52:29Z
- Package: uvicorn
- Fixed: 0.21.0
- Mode: fixed_only
- Outcome: ok
Logs
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“I just reproduced the issue and confirmed the code 1006 is used even after commenting out The terminal output”
“- This was closed by #1737. It will be available on Uvicorn 0.20.0.”
“Can you please try with another client? The server logs point out that we are sending 1012, which is the intention.”
“I've tested with https://github.com/websockets/wscat, and indeed, this is still happening, but only on websockets implementation. The wsproto works as expected.”
Failure Signature (Search String)
- Incorrect websocket close code on server shutdown
- 2. Run `uvicorn --ws-ping-interval 5 --ws-ping-timeout 5 --log-level debug --lifespan off websocket_test:App`
Copy-friendly signature
Failure Signature
-----------------
Incorrect websocket close code on server shutdown
2. Run `uvicorn --ws-ping-interval 5 --ws-ping-timeout 5 --log-level debug --lifespan off websocket_test:App`
Error Message
Signature-only (no traceback captured)
Error Message
-------------
Incorrect websocket close code on server shutdown
2. Run `uvicorn --ws-ping-interval 5 --ws-ping-timeout 5 --log-level debug --lifespan off websocket_test:App`
Minimal Reproduction
class App:
def __init__(self, scope, receive, send):
self.scope = scope
self.receive = receive
self.send = send
def __await__(self):
return self.asgi().__await__()
async def asgi(self):
while True:
message = await self.receive()
message_type = message["type"].replace(".", "_")
print(message)
handler = getattr(self, message_type, None)
if handler is not None:
await handler(message)
if message_type == "websocket_disconnect":
break
async def websocket_connect(self, message):
await self.send({"type": "websocket.accept"})
Environment
- Python: 3.10.1
What Broke
Clients experience abnormal disconnections with code 1006 instead of the expected 1001.
Why It Broke
The websocket close code 1006 is sent instead of the expected 1001 during server shutdown
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
Upgrade to version 0.21.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/1816
First fixed release: 0.21.0
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix should not be applied if the server behavior is expected to remain unchanged.
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
- Add a TLS smoke test that performs a real handshake in CI (include CA bundle validation and hostname checks).
- Alert on handshake failures by error string and endpoint to catch cert/CA changes quickly.
- Make timeouts explicit and test them (unit + integration) to avoid silent behavior changes.
- Instrument retries (attempt count + reason) and alert on spikes to catch dependency slowdowns.
Version Compatibility Table
| Version | Status |
|---|---|
| 0.21.0 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.