This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author hyzyla
Recipients asvetlov, hyzyla, yselivanov
Date 2021-12-06.14:04:23
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1638799463.93.0.445533986487.issue45997@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2021-12-06 14:04:23hyzylasetrecipients: + hyzyla, asvetlov, yselivanov
2021-12-06 14:04:23hyzylasetmessageid: <1638799463.93.0.445533986487.issue45997@roundup.psfhosted.org>
2021-12-06 14:04:23hyzylalinkissue45997 messages
2021-12-06 14:04:23hyzylacreate