python runtime_error ai_generated true

RuntimeError: Cannot call 'receive' once a disconnect message has been received.

ID: python/starlette-websocket-receive

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2025-11-25First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
0.30.x active
0.40.x active

Root Cause

Attempting to receive more messages after the WebSocket client has disconnected.

generic

中文

在 WebSocket 客户端断开连接后尝试接收更多消息。

Workarounds

  1. 95% success
    while True:
        try:
            data = await websocket.receive_text()
        except WebSocketDisconnect:
            break
  2. 90% success
    if websocket.client_state == WebSocketState.CONNECTED:
        data = await websocket.receive_text()

Dead Ends

Common approaches that don't work:

  1. 70% fail

    May cause infinite loop if not breaking.

  2. 80% fail

    Leaves the connection in an inconsistent state.