{
  "id": "python/asyncio-invalid-state-semaphore",
  "signature": "RuntimeError: Semaphore released too many times",
  "signature_zh": "运行时错误：信号量释放次数过多",
  "regex": "RuntimeError:\\ Semaphore\\ released\\ too\\ many\\ times",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A semaphore was released more times than it was acquired, often due to releasing in a finally block without checking if acquired.",
  "root_cause_type": "generic",
  "root_cause_zh": "信号量被释放的次数超过了获取次数，通常是由于在 finally 块中释放而未检查是否已获取。",
  "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"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Increasing the initial semaphore value",
      "why_fails": "This masks the bug but does not fix the logic; over-release still occurs.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using semaphore.acquire() without await",
      "why_fails": "acquire() returns a coroutine; not awaiting it leads to incorrect behavior.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use context manager for semaphore to ensure proper acquire/release",
      "success_rate": 0.95,
      "how": "async with semaphore: await some_coroutine()",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use a try-finally block with a flag to track acquisition",
      "success_rate": 0.9,
      "how": "acquired = await semaphore.acquire(); try: await some_coroutine(); finally: if acquired: 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.84,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-06-20",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}