{
  "id": "python/asyncio-future-attached-to-loop",
  "signature": "RuntimeError: Task <Task pending> got Future <Future pending> attached to a different loop",
  "signature_zh": "运行时错误：任务 <Task pending> 获取了附加到不同循环的 Future <Future pending>",
  "regex": "RuntimeError:\\ Task\\ <Task\\ pending>\\ got\\ Future\\ <Future\\ pending>\\ attached\\ to\\ a\\ different\\ loop",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A coroutine or task awaits a Future that belongs to a different event loop, often due to mixing loops across threads or using asyncio.gather with tasks from different loops.",
  "root_cause_type": "generic",
  "root_cause_zh": "协程或任务等待了一个属于不同事件循环的 Future，通常是由于跨线程混合循环或在 asyncio.gather 中使用来自不同循环的任务。",
  "versions": [
    {
      "version": "3.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.10",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Creating a new loop and setting it globally",
      "why_fails": "The Future is still attached to the original loop; global setting does not migrate it.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using asyncio.ensure_future instead of create_task",
      "why_fails": "ensure_future also attaches to the current loop; same issue.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Ensure all coroutines and futures are created in the same event loop",
      "success_rate": 0.9,
      "how": "loop = asyncio.new_event_loop(); asyncio.set_event_loop(loop); loop.run_until_complete(main())",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use asyncio.run() to avoid loop conflicts entirely",
      "success_rate": 0.95,
      "how": "async def main(): await asyncio.gather(coro1(), coro2()); asyncio.run(main())",
      "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.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-10-10",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}