{
  "id": "python/starlette-testclient-blocking-async",
  "signature": "RuntimeError: Cannot run the event loop while another loop is running",
  "signature_zh": "RuntimeError: 当另一个循环正在运行时无法运行事件循环",
  "regex": "RuntimeError:\\ Cannot\\ run\\ the\\ event\\ loop\\ while\\ another\\ loop\\ is\\ running",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Calling asyncio.run() or loop.run_until_complete inside an async test that already has a running loop, often mixing TestClient and AsyncClient.",
  "root_cause_type": "generic",
  "root_cause_zh": "在已有运行循环的异步测试中调用 asyncio.run() 或 loop.run_until_complete，常见于混用 TestClient 和 AsyncClient。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Patches loop globally and can mask other async bugs.",
      "fail_rate": 0.55,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "TestClient blocks; cannot test concurrent async behavior.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.92,
      "how": "Use httpx.AsyncClient(transport=ASGITransport(app=app), base_url='http://test') with pytest.mark.asyncio",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "await the call directly: response = await client.get('/')",
      "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.86,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-11-02",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}