{
  "id": "python/asyncio-semaphore-acquire-error",
  "signature": "ValueError: Semaphore acquired too many times",
  "signature_zh": "值错误：信号量获取次数过多",
  "regex": "ValueError:\\ Semaphore\\ acquired\\ too\\ many\\ times",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "An asyncio.Semaphore is released more times than it was acquired, causing the counter to exceed the initial value.",
  "root_cause_type": "generic",
  "root_cause_zh": "asyncio.Semaphore 的释放次数多于获取次数，导致计数器超过初始值。",
  "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"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Increasing the semaphore limit to avoid the error",
      "why_fails": "This masks the imbalance bug and may lead to resource exhaustion.",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using a try-finally block that releases unconditionally",
      "why_fails": "If the semaphore was not acquired due to an exception, releasing it causes the error.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use async with for semaphore to ensure balanced acquire/release",
      "success_rate": 0.95,
      "how": "async with semaphore:\n    # limited resource access",
      "condition": "",
      "sources": []
    },
    {
      "action": "Track acquire count manually to avoid double release",
      "success_rate": 0.9,
      "how": "acquired = False\ntry:\n    await semaphore.acquire()\n    acquired = True\n    # use resource\nfinally:\n    if acquired:\n        semaphore.release()",
      "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-02-14",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}