{
  "id": "python/starlette-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": "Async resources (e.g., aiohttp/httpx client, DB pool) created in one event loop are reused after that loop is closed, often across test cases or reloads.",
  "root_cause_type": "generic",
  "root_cause_zh": "在某个事件循环中创建的异步资源（如 aiohttp/httpx 客户端、数据库连接池）在该循环关闭后被复用，常见于测试用例或热重载。",
  "versions": [
    {
      "version": "0.30+",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Replaces loop but leaves stale client bound to old loop.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Buffering setting irrelevant to event loop lifecycle.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.92,
      "how": "from contextlib import asynccontextmanager\n@asynccontextmanager\nasync def lifespan(app):\n    app.state.client = httpx.AsyncClient()\n    yield\n    await app.state.client.aclose()\napp = FastAPI(lifespan=lifespan)",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "async with httpx.AsyncClient() as client:\n    r = await client.get(url)",
      "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-08-14",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}