{
  "id": "python/starlette-websocket-connection-closed",
  "signature": "RuntimeError: WebSocket connection closed unexpectedly",
  "signature_zh": "运行时错误：WebSocket连接意外关闭",
  "regex": "RuntimeError:\\ WebSocket\\ connection\\ closed\\ unexpectedly",
  "domain": "python",
  "category": "network_error",
  "subcategory": null,
  "root_cause": "客户端或服务器端非正常关闭WebSocket连接",
  "root_cause_type": "generic",
  "root_cause_zh": "客户端或服务器端非正常关闭WebSocket连接",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "尝试重新连接而不处理关闭原因",
      "why_fails": "未处理关闭原因可能导致无限重连",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "在接收消息时不检查连接状态",
      "why_fails": "已关闭的连接无法接收消息",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "在WebSocket处理中添加重连逻辑",
      "success_rate": 0.9,
      "how": "async def websocket_endpoint(websocket):\n    await websocket.accept()\n    try:\n        while True:\n            data = await websocket.receive_text()\n    except WebSocketDisconnect:\n        print('Client disconnected')",
      "condition": "",
      "sources": []
    },
    {
      "action": "使用心跳检测保持连接",
      "success_rate": 0.85,
      "how": "async def heartbeat(websocket):\n    while True:\n        await asyncio.sleep(30)\n        await websocket.send_text('ping')",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.82,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-08-20",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}