{
  "id": "python/event-loop-closed-runtimeerror",
  "signature": "RuntimeError: Event loop is closed",
  "signature_zh": "RuntimeError: 事件循环已关闭",
  "regex": "RuntimeError:\\ Event\\ loop\\ is\\ closed",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "An asyncio coroutine or aiohttp session is reused after the event loop that created it has been closed. Common when calling asyncio.run() multiple times, or when a ClientSession is created outside the loop and used inside a different asyncio.run() call. The underlying transport/socket is bound to a now-dead loop.",
  "root_cause_type": "generic",
  "root_cause_zh": "在创建它的 asyncio 事件循环已关闭后，仍复用了协程或 aiohttp 会话。常见于多次调用 asyncio.run()，或在循环外创建 ClientSession 却在另一次 asyncio.run() 内使用。底层 transport/socket 绑定在已失效的循环上。",
  "versions": [
    {
      "version": "3.8+",
      "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"
    },
    {
      "version": "3.12",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Silencing the exception leaves the coroutine unfinished and leaks resources; the next call fails differently or hangs.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "A closed loop cannot be restarted; run_until_complete raises RuntimeError immediately.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Policy changes do not resurrect a closed loop; the error is lifecycle, not platform.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "async def main():\n    async with aiohttp.ClientSession() as session:\n        async with session.get(url) as r:\n            return await r.text()\n\nasyncio.run(main())  # session lives and dies inside this loop",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "import asyncio\nloop = asyncio.new_event_loop()\nasyncio.set_event_loop(loop)\ntry:\n    loop.run_until_complete(main())\nfinally:\n    loop.run_until_complete(loop.shutdown_asyncgens())\n    loop.close()",
      "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-03-12",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}