diff options
author | Andrew Svetlov <andrew.svetlov@gmail.com> | 2022-03-22 14:02:51 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-22 14:02:51 (GMT) |
commit | 32e77154ddfc514a3144d5912bffdd957246fd6c (patch) | |
tree | 862472617c97dc822ea1dbc33cd3e0396090ebb1 /Lib/asyncio | |
parent | 673755bfbac46b3cd2c84d7e0d68c2c488e039c3 (diff) | |
download | cpython-32e77154ddfc514a3144d5912bffdd957246fd6c.zip cpython-32e77154ddfc514a3144d5912bffdd957246fd6c.tar.gz cpython-32e77154ddfc514a3144d5912bffdd957246fd6c.tar.bz2 |
bpo-45997: Fix asyncio.Semaphore re-acquiring order (GH-31910)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Diffstat (limited to 'Lib/asyncio')
-rw-r--r-- | Lib/asyncio/locks.py | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py index 0fbccfa..9b46121 100644 --- a/Lib/asyncio/locks.py +++ b/Lib/asyncio/locks.py @@ -6,6 +6,7 @@ import collections from . import exceptions from . import mixins +from . import tasks class _ContextManagerMixin: @@ -346,6 +347,7 @@ class Semaphore(_ContextManagerMixin, mixins._LoopBoundMixin): 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__() @@ -359,6 +361,7 @@ class Semaphore(_ContextManagerMixin, mixins._LoopBoundMixin): waiter = self._waiters.popleft() if not waiter.done(): waiter.set_result(None) + self._wakeup_scheduled = True return def locked(self): @@ -374,16 +377,17 @@ class Semaphore(_ContextManagerMixin, mixins._LoopBoundMixin): 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 |