{
  "id": "python/asyncio-semaphore-bound-in-different-loop",
  "signature": "RuntimeError: <asyncio.locks.Semaphore object at 0x...> is bound to a different event loop",
  "signature_zh": "RuntimeError: <asyncio.locks.Semaphore object at 0x...> 绑定到不同的事件循环",
  "regex": "RuntimeError:\\ <asyncio\\.locks\\.Semaphore\\ object\\ at\\ 0x\\.\\.\\.>\\ is\\ bound\\ to\\ a\\ different\\ event\\ loop",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "An asyncio synchronization primitive (Semaphore, Lock, Event, Queue) was created under one loop and used under another, common when module-level primitives are reused across asyncio.run() calls.",
  "root_cause_type": "generic",
  "root_cause_zh": "在某个事件循环下创建的 asyncio 同步原语（Semaphore、Lock、Event、Queue）被另一个循环使用，常见于跨 asyncio.run() 调用复用模块级原语。",
  "versions": [
    {
      "version": "3.8+",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.9+",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "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": "Private attribute hack; the semaphore's internal waiters still reference the old loop.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The primitive is still bound to its original loop; new loop does not fix it.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "async def main():\n    sem = asyncio.Semaphore(10)  # created inside the loop\n    await run_all(sem)\n\nasyncio.run(main())",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "loop = asyncio.new_event_loop()\nasyncio.set_event_loop(loop)\nloop.run_until_complete(main())  # reuse loop; do not call asyncio.run() repeatedly",
      "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.87,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-12-03",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}