python
runtime_error
ai_generated
true
RuntimeError:收到断开连接消息后无法调用 'receive'。
RuntimeError: Cannot call 'receive' once a disconnect message has been received.
ID: python/starlette-websocket-receive
80%修复率
87%置信度
0证据数
2025-11-25首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 0.30.x | active | — | — | — |
| 0.40.x | active | — | — | — |
根因分析
在 WebSocket 客户端断开连接后尝试接收更多消息。
English
Attempting to receive more messages after the WebSocket client has disconnected.
解决方案
-
95% 成功率
while True: try: data = await websocket.receive_text() except WebSocketDisconnect: break -
90% 成功率
if websocket.client_state == WebSocketState.CONNECTED: data = await websocket.receive_text()
无效尝试
常见但无效的做法:
-
70% 失败
May cause infinite loop if not breaking.
-
80% 失败
Leaves the connection in an inconsistent state.