mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-45997: Fix asyncio.Semaphore re-acquiring order (GH-31910)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
This commit is contained in:
parent
673755bfba
commit
32e77154dd
3 changed files with 36 additions and 6 deletions
|
@ -6,6 +6,7 @@ import collections
|
||||||
|
|
||||||
from . import exceptions
|
from . import exceptions
|
||||||
from . import mixins
|
from . import mixins
|
||||||
|
from . import tasks
|
||||||
|
|
||||||
|
|
||||||
class _ContextManagerMixin:
|
class _ContextManagerMixin:
|
||||||
|
@ -346,6 +347,7 @@ class Semaphore(_ContextManagerMixin, mixins._LoopBoundMixin):
|
||||||
raise ValueError("Semaphore initial value must be >= 0")
|
raise ValueError("Semaphore initial value must be >= 0")
|
||||||
self._value = value
|
self._value = value
|
||||||
self._waiters = collections.deque()
|
self._waiters = collections.deque()
|
||||||
|
self._wakeup_scheduled = False
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
res = super().__repr__()
|
res = super().__repr__()
|
||||||
|
@ -359,6 +361,7 @@ class Semaphore(_ContextManagerMixin, mixins._LoopBoundMixin):
|
||||||
waiter = self._waiters.popleft()
|
waiter = self._waiters.popleft()
|
||||||
if not waiter.done():
|
if not waiter.done():
|
||||||
waiter.set_result(None)
|
waiter.set_result(None)
|
||||||
|
self._wakeup_scheduled = True
|
||||||
return
|
return
|
||||||
|
|
||||||
def locked(self):
|
def locked(self):
|
||||||
|
@ -374,16 +377,17 @@ class Semaphore(_ContextManagerMixin, mixins._LoopBoundMixin):
|
||||||
called release() to make it larger than 0, and then return
|
called release() to make it larger than 0, and then return
|
||||||
True.
|
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()
|
fut = self._get_loop().create_future()
|
||||||
self._waiters.append(fut)
|
self._waiters.append(fut)
|
||||||
try:
|
try:
|
||||||
await fut
|
await fut
|
||||||
except:
|
# reset _wakeup_scheduled *after* waiting for a future
|
||||||
# See the similar code in Queue.get.
|
self._wakeup_scheduled = False
|
||||||
fut.cancel()
|
except exceptions.CancelledError:
|
||||||
if self._value > 0 and not fut.cancelled():
|
self._wake_up_next()
|
||||||
self._wake_up_next()
|
|
||||||
raise
|
raise
|
||||||
self._value -= 1
|
self._value -= 1
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -917,6 +917,31 @@ class SemaphoreTests(unittest.IsolatedAsyncioTestCase):
|
||||||
sem.release()
|
sem.release()
|
||||||
self.assertFalse(sem.locked())
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix :class:`asyncio.Semaphore` re-aquiring FIFO order.
|
Loading…
Add table
Add a link
Reference in a new issue