python network_error ai_generated true

starlette.websockets.WebSocketDisconnect: 1000

ID: python/starlette-websocket-disconnect-receive

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2025-03-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
0.30.x active — — —
0.36.x active — — —

Root Cause

Client closed the WebSocket while the server was awaiting receive_text/receive_json.

generic

中文

服务器在等待receive_text/receive_json时客户端关闭了WebSocket。

Workarounds

  1. 95% success
    try:\n    msg = await ws.receive_text()\nexcept WebSocketDisconnect:\n    return
  2. 92% success
    try:\n    while True: ...\nfinally:\n    await manager.disconnect(ws)

Dead Ends

Common approaches that don't work:

  1. 85% fail

    Socket is closed; next receive raises again immediately.

  2. 80% fail

    Does not prevent application-level disconnects.