# asyncio.exceptions.TimeoutError: (deadlock in acquire) semaphore held across await

- **ID:** `python/asyncio-semaphore-deadlock`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |
| 3.11 | active | — | — |
| 3.12 | active | — | — |

## Workarounds

1. **** (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)
   ```
2. **** (75% success)
   ```
   Use a re-entrant pattern with contextvars to track ownership.
   ```
3. **** (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

- **** — If the same task re-enters, it still deadlocks at count+1. (70% fail)
- **** — Detects the deadlock but doesn't fix the logic; the underlying work never completes. (60% fail)
- **** — Same re-entrancy problem, now with count=1. (85% fail)
