{
  "id": "python/starlette-websocket-accept-before-receive",
  "signature": "RuntimeError: Cannot call 'receive' before 'accept' on a WebSocket",
  "signature_zh": "运行时错误：不能在 WebSocket 上调用 'receive' 之前调用 'accept'",
  "regex": "RuntimeError:\\ Cannot\\ call\\ 'receive'\\ before\\ 'accept'\\ on\\ a\\ WebSocket",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Starlette WebSocket 在未调用 accept 方法前尝试接收数据",
  "root_cause_type": "generic",
  "root_cause_zh": "Starlette WebSocket 在未调用 accept 方法前尝试接收数据",
  "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": "在 accept 前发送数据",
      "why_fails": "同样需要先 accept",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "忽略 accept 直接使用 receive",
      "why_fails": "WebSocket 连接未建立",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "先调用 accept 再接收数据",
      "success_rate": 0.95,
      "how": "await websocket.accept()\ndata = await websocket.receive_text()",
      "condition": "",
      "sources": []
    },
    {
      "action": "在异步上下文管理器中处理",
      "success_rate": 0.9,
      "how": "async with websocket:\n    await websocket.accept()\n    async for message in websocket:\n        ...",
      "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": "2025-08-01",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}