{
  "id": "python/fastapi-sync-endpoint-blocking",
  "signature": "RuntimeError: Task got Future attached to a different loop",
  "signature_zh": "运行时错误：任务获取了附加到不同事件循环的 Future",
  "regex": "RuntimeError:\\ Task\\ got\\ Future\\ attached\\ to\\ a\\ different\\ loop",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "在 FastAPI 的同步端点中使用了异步库或跨线程操作，导致事件循环冲突。",
  "root_cause_type": "generic",
  "root_cause_zh": "在 FastAPI 的同步端点中使用了异步库或跨线程操作，导致事件循环冲突。",
  "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.3,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "线程和事件循环不兼容，导致 Future 跨循环。",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.85,
      "how": "from starlette.concurrency import run_in_threadpool; result = await run_in_threadpool(sync_func)",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.95,
      "how": "async def endpoint(): await async_func()",
      "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-08-02",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}