{
  "id": "python/event-loop-closed",
  "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 async operation was attempted after the event loop had already been closed, typically when reusing a loop object across multiple asyncio.run() calls or after calling loop.close() manually.",
  "root_cause_type": "generic",
  "root_cause_zh": "在事件循环已关闭后仍尝试执行异步操作，通常是因为在多次调用 asyncio.run() 时复用了同一个 loop 对象，或手动调用了 loop.close()。",
  "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"
    },
    {
      "version": "3.11",
      "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": "Creates a new loop but the pending coroutine/task still references the old closed loop's internals, causing the same error deeper in the stack.",
      "fail_rate": 0.65,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Swallows the error but the awaited work never completes, leading to silent data loss downstream.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Raises the same RuntimeError immediately because the loop state is terminal.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Use asyncio.run(main()) as the single entry point and never call loop.close() manually. asyncio.run creates and closes the loop for you.\n\nasync def main():\n    ...\n\nif __name__ == '__main__':\n    asyncio.run(main())",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "If you must reuse a loop (e.g. in tests), use asyncio.new_event_loop() per test and call loop.close() only in teardown:\n\nloop = asyncio.new_event_loop()\nasyncio.set_event_loop(loop)\ntry:\n    loop.run_until_complete(coro())\nfinally:\n    loop.close()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "For aiohttp/aiofiles contexts, ensure the ClientSession is created and closed inside the same asyncio.run scope:\n\nasync def main():\n    async with aiohttp.ClientSession() as s:\n        await s.get(url)\nasyncio.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-03-12",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}