# ValueError: Semaphore acquired too many times

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

## Root Cause

An asyncio.Semaphore is released more times than it was acquired, causing the counter to exceed the initial value.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |

## Workarounds

1. **Use async with for semaphore to ensure balanced acquire/release** (95% success)
   ```
   async with semaphore:
    # limited resource access
   ```
2. **Track acquire count manually to avoid double release** (90% success)
   ```
   acquired = False
try:
    await semaphore.acquire()
    acquired = True
    # use resource
finally:
    if acquired:
        semaphore.release()
   ```

## Dead Ends

- **Increasing the semaphore limit to avoid the error** — This masks the imbalance bug and may lead to resource exhaustion. (40% fail)
- **Using a try-finally block that releases unconditionally** — If the semaphore was not acquired due to an exception, releasing it causes the error. (60% fail)
