{
  "id": "python/asyncio-semaphore-deadlock",
  "signature": "asyncio.exceptions.TimeoutError: (deadlock in acquire) semaphore held across await",
  "signature_zh": "asyncio.exceptions.TimeoutError: （获取时死锁）信号量跨 await 持有",
  "regex": "asyncio\\.exceptions\\.TimeoutError:\\ \\(deadlock\\ in\\ acquire\\)\\ semaphore\\ held\\ across\\ await",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A coroutine acquired an asyncio.Semaphore and then awaited another coroutine that also tried to acquire the same semaphore, causing a self-deadlock.",
  "root_cause_type": "generic",
  "root_cause_zh": "某个协程获取了 asyncio.Semaphore，随后 await 另一个也尝试获取同一信号量的协程，导致自我死锁。",
  "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": "If the same task re-enters, it still deadlocks at count+1.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Detects the deadlock but doesn't fix the logic; the underlying work never completes.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Same re-entrancy problem, now with count=1.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Never hold a semaphore across another acquire of the same semaphore; restructure so the inner call uses a separate limiter:\n\nouter = asyncio.Semaphore(10)\ninner = asyncio.Semaphore(10)",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.75,
      "how": "Use a re-entrant pattern with contextvars to track ownership.",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "Add structured logging around acquire/release to find the cycle:\n\nasync with sem:\n    logger.info('acquired', extra={'task': id(asyncio.current_task())})",
      "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.83,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-10-08",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}