{
  "id": "python/unittest-isolated-asyncio-testcase",
  "signature": "RuntimeError: There is no current event loop in thread 'MainThread'.",
  "signature_zh": "RuntimeError: 线程 'MainThread' 中没有当前事件循环。",
  "regex": "RuntimeError:\\ There\\ is\\ no\\ current\\ event\\ loop\\ in\\ thread\\ 'MainThread'\\.",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Using unittest.IsolatedAsyncioTestCase with code that calls asyncio.get_event_loop() from a non-async context, or mixing sync and async test methods incorrectly.",
  "root_cause_type": "generic",
  "root_cause_zh": "在 unittest.IsolatedAsyncioTestCase 中使用在非异步上下文调用 asyncio.get_event_loop() 的代码，或错误混合同步/异步测试方法。",
  "versions": [
    {
      "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": "IsolatedAsyncioTestCase manages its own loop; overriding it causes 'loop already running' or leaks loops between tests.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Works for simple cases but breaks async fixtures and shares no loop with the test's async methods.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "IsolatedAsyncioTestCase exists from 3.8; downgrading does not change the event-loop lookup behavior.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "class TestX(unittest.IsolatedAsyncioTestCase):\n    async def test_y(self):\n        loop = asyncio.get_running_loop()\n        await loop.create_task(coro())",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "class TestX(unittest.IsolatedAsyncioTestCase):\n    async def test_y(self):\n        loop = asyncio.get_running_loop()\n        result = await my_func(loop=loop)\n        self.assertEqual(result, 1)",
      "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": "2025-03-07",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}