{
  "id": "python/pytest-asyncio-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": "With pytest-asyncio, the default function-scoped event loop is torn down after the first async test, but a session-scoped async fixture or a lingering task still references the closed loop.",
  "root_cause_type": "generic",
  "root_cause_zh": "使用 pytest-asyncio 时，默认的函数级事件循环在第一个异步测试后被销毁，但会话级的异步 fixture 或残留任务仍引用已关闭的循环。",
  "versions": [
    {
      "version": "0.23.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "0.24.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "0.25.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "pytest-asyncio already manages the loop; creating a new one mid-test orphans the fixture's loop and the error persists on teardown.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The marker is likely already present; the problem is loop scope mismatch, not the missing marker.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "# pytest.ini\n[pytest]\nasyncio_mode = auto\nasyncio_default_fixture_loop_scope = session",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "# conftest.py\nimport asyncio, pytest\n\n@pytest.fixture(scope='session')\ndef event_loop():\n    loop = asyncio.new_event_loop()\n    yield loop\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.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-05-20",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}