{
  "id": "python/starlette-websocket-disconnect-error",
  "signature": "websockets.exceptions.ConnectionClosedError: WebSocket connection closed unexpectedly",
  "signature_zh": "WebSocket 连接意外关闭错误",
  "regex": "websockets\\.exceptions\\.ConnectionClosedError:\\ WebSocket\\ connection\\ closed\\ unexpectedly",
  "domain": "python",
  "category": "network_error",
  "subcategory": null,
  "root_cause": "Starlette WebSocket 连接因网络问题或客户端断开而意外关闭",
  "root_cause_type": "generic",
  "root_cause_zh": "Starlette WebSocket 连接因网络问题或客户端断开而意外关闭",
  "versions": [
    {
      "version": "3.10",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.11",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "忽略异常并继续发送消息",
      "why_fails": "连接已关闭，发送消息会引发更多错误",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "无限重试连接而不检查状态",
      "why_fails": "可能导致无限循环和资源浪费",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "捕获异常并优雅地关闭连接",
      "success_rate": 0.9,
      "how": "try:\n    await websocket.receive_text()\nexcept WebSocketDisconnect:\n    await websocket.close()",
      "condition": "",
      "sources": []
    },
    {
      "action": "实现心跳机制检测连接状态",
      "success_rate": 0.85,
      "how": "async def heartbeat():\n    while True:\n        await asyncio.sleep(30)\n        await websocket.send_json({'type': '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.84,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-11-05",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}