Message407809
Class `asyncio.Semaphore` has dequeue in implementation for waiters of semaphore and seems like the intention of this dequeue is to maintain FIFO queue of waiters. But it doesn't work, coroutine that releases semaphore can acquire semaphore again. Below is reproducible example:
```python
import asyncio
async def process(idx: int, semaphore: asyncio.Semaphore) -> None:
while True:
async with semaphore:
print(f'ACQUIRE {idx}')
await asyncio.sleep(1)
async def main() -> None:
semaphore = asyncio.Semaphore(5)
await asyncio.gather(*[process(idx, semaphore) for idx in range(20)])
asyncio.run(main())
```
In console:
```
/home/y.hyzyla/Work/asynciosemaphorebug/venv/bin/python /home/y.hyzyla/Work/asynciosemaphorebug/main.py
ACQUIRE 0
ACQUIRE 1
ACQUIRE 2
ACQUIRE 3
ACQUIRE 4
ACQUIRE 0
ACQUIRE 1
ACQUIRE 2
ACQUIRE 3
ACQUIRE 4
ACQUIRE 0
ACQUIRE 1
ACQUIRE 2
ACQUIRE 3
ACQUIRE 4
....
```
Ugly fix, is to add asyncio.sleep right before semaphore.
```python
while True:
await asyncio.sleep(0)
async with semaphore:
...
```
Also, I found a comment on Stack Overflow about race condition in Semaphore implementation, but I don't know about the quality of that comment:
https://stackoverflow.com/questions/55951233/does-pythons-asyncio-lock-acquire-maintain-order#comment112978366_55951304 |
|
Date |
User |
Action |
Args |
2021-12-06 14:04:23 | hyzyla | set | recipients:
+ hyzyla, asvetlov, yselivanov |
2021-12-06 14:04:23 | hyzyla | set | messageid: <1638799463.93.0.445533986487.issue45997@roundup.psfhosted.org> |
2021-12-06 14:04:23 | hyzyla | link | issue45997 messages |
2021-12-06 14:04:23 | hyzyla | create | |
|