{
  "id": "python/asyncio-event-loop-is-running-pytest",
  "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": "Since Python 3.10, asyncio.get_event_loop() no longer creates a loop implicitly when called from a thread with no running loop; code that relied on the old behavior raises this error, common in pytest and background threads.",
  "root_cause_type": "generic",
  "root_cause_zh": "自 Python 3.10 起，在没有运行循环的线程中调用 asyncio.get_event_loop() 不再隐式创建循环；依赖旧行为的代码会抛出此错误，常见于 pytest 和后台线程。",
  "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": "Blocks upgrades and the behavior is slated for removal; not a durable fix.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Makes get_event_loop raise immediately; does not provide a usable loop.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "loop = asyncio.new_event_loop()\nasyncio.set_event_loop(loop)\ntry:\n    loop.run_until_complete(main())\nfinally:\n    loop.close()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.92,
      "how": "# pytest.ini: asyncio_mode = auto\nimport pytest\n@pytest.mark.asyncio\nasync def test_foo():\n    assert await foo() == 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.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-03-06",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}