From 0795aca5cb1f953cce26b2efa973c4bd6a630649 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Tue, 22 Mar 2022 16:02:51 +0200 Subject: [PATCH 1/2] bpo-45997: Fix asyncio.Semaphore re-acquiring order (GH-31910) Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> (cherry picked from commit 32e77154ddfc514a3144d5912bffdd957246fd6c) Co-authored-by: Andrew Svetlov --- Lib/asyncio/locks.py | 16 +++++++----- Lib/test/test_asyncio/test_locks.py | 25 +++++++++++++++++++ .../2022-03-15-18-32-12.bpo-45997.4n2aVU.rst | 1 + 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2022-03-15-18-32-12.bpo-45997.4n2aVU.rst diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py index 4fef64e3921e17..7b81c25b2d9e76 100644 --- a/Lib/asyncio/locks.py +++ b/Lib/asyncio/locks.py @@ -6,6 +6,7 @@ from . import exceptions from . import mixins +from . import tasks class _ContextManagerMixin: @@ -350,6 +351,7 @@ def __init__(self, value=1, *, loop=mixins._marker): raise ValueError("Semaphore initial value must be >= 0") self._value = value self._waiters = collections.deque() + self._wakeup_scheduled = False def __repr__(self): res = super().__repr__() @@ -363,6 +365,7 @@ def _wake_up_next(self): waiter = self._waiters.popleft() if not waiter.done(): waiter.set_result(None) + self._wakeup_scheduled = True return def locked(self): @@ -378,16 +381,17 @@ async def acquire(self): called release() to make it larger than 0, and then return True. """ - while self._value <= 0: + # _wakeup_scheduled is set if *another* task is scheduled to wakeup + # but its acquire() is not resumed yet + while self._wakeup_scheduled or self._value <= 0: fut = self._get_loop().create_future() self._waiters.append(fut) try: await fut - except: - # See the similar code in Queue.get. - fut.cancel() - if self._value > 0 and not fut.cancelled(): - self._wake_up_next() + # reset _wakeup_scheduled *after* waiting for a future + self._wakeup_scheduled = False + except exceptions.CancelledError: + self._wake_up_next() raise self._value -= 1 return True diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py index e2cd2ba0365fda..91b3cf2ded76bf 100644 --- a/Lib/test/test_asyncio/test_locks.py +++ b/Lib/test/test_asyncio/test_locks.py @@ -933,6 +933,31 @@ async def test_release_no_waiters(self): sem.release() self.assertFalse(sem.locked()) + async def test_acquire_fifo_order(self): + sem = asyncio.Semaphore(1) + result = [] + + async def coro(tag): + await sem.acquire() + result.append(f'{tag}_1') + await asyncio.sleep(0.01) + sem.release() + + await sem.acquire() + result.append(f'{tag}_2') + await asyncio.sleep(0.01) + sem.release() + + async with asyncio.TaskGroup() as tg: + tg.create_task(coro('c1')) + tg.create_task(coro('c2')) + tg.create_task(coro('c3')) + + self.assertEqual( + ['c1_1', 'c2_1', 'c3_1', 'c1_2', 'c2_2', 'c3_2'], + result + ) + if __name__ == '__main__': unittest.main() diff --git a/Misc/NEWS.d/next/Library/2022-03-15-18-32-12.bpo-45997.4n2aVU.rst b/Misc/NEWS.d/next/Library/2022-03-15-18-32-12.bpo-45997.4n2aVU.rst new file mode 100644 index 00000000000000..40d8504e5a946b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-03-15-18-32-12.bpo-45997.4n2aVU.rst @@ -0,0 +1 @@ +Fix :class:`asyncio.Semaphore` re-aquiring FIFO order. From 59b4029b1675b4890cbd61a6b5608d397459f0b2 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Tue, 22 Mar 2022 16:32:52 +0200 Subject: [PATCH 2/2] Fix tests --- Lib/test/test_asyncio/test_locks.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py index 91b3cf2ded76bf..c5e3fdcbc955a5 100644 --- a/Lib/test/test_asyncio/test_locks.py +++ b/Lib/test/test_asyncio/test_locks.py @@ -948,10 +948,11 @@ async def coro(tag): await asyncio.sleep(0.01) sem.release() - async with asyncio.TaskGroup() as tg: - tg.create_task(coro('c1')) - tg.create_task(coro('c2')) - tg.create_task(coro('c3')) + t1 = asyncio.create_task(coro('c1')) + t2 = asyncio.create_task(coro('c2')) + t3 = asyncio.create_task(coro('c3')) + + await asyncio.gather(t1, t2, t3) self.assertEqual( ['c1_1', 'c2_1', 'c3_1', 'c1_2', 'c2_2', 'c3_2'],