python
runtime_error
ai_generated
true
asyncio.exceptions.TimeoutError: (deadlock in acquire) semaphore held across await
ID: python/asyncio-semaphore-deadlock
80%Fix Rate
83%Confidence
0Evidence
2024-10-08First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
| 3.12 | active | — | — | — |
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.
generic中文
某个协程获取了 asyncio.Semaphore,随后 await 另一个也尝试获取同一信号量的协程,导致自我死锁。
Workarounds
-
90% success
Never hold a semaphore across another acquire of the same semaphore; restructure so the inner call uses a separate limiter: outer = asyncio.Semaphore(10) inner = asyncio.Semaphore(10)
-
75% success
Use a re-entrant pattern with contextvars to track ownership.
-
85% success
Add structured logging around acquire/release to find the cycle: async with sem: logger.info('acquired', extra={'task': id(asyncio.current_task())})
Dead Ends
Common approaches that don't work:
-
70% fail
If the same task re-enters, it still deadlocks at count+1.
-
60% fail
Detects the deadlock but doesn't fix the logic; the underlying work never completes.
-
85% fail
Same re-entrancy problem, now with count=1.