diff options
author | Bar Harel <bzvi7919@gmail.com> | 2018-02-02 23:15:31 (GMT) |
---|---|---|
committer | Yury Selivanov <yury@magic.io> | 2018-02-02 23:15:31 (GMT) |
commit | 7e4cf8e95d2971ae0d5fb417152183070184293f (patch) | |
tree | 4d83f97a0695d0b33846e6e10394bb83337a88ce /Lib/asyncio | |
parent | fbf8e823c02ac1c93a48609cc74e439e19ccb426 (diff) | |
download | cpython-7e4cf8e95d2971ae0d5fb417152183070184293f.zip cpython-7e4cf8e95d2971ae0d5fb417152183070184293f.tar.gz cpython-7e4cf8e95d2971ae0d5fb417152183070184293f.tar.bz2 |
[3.6] bpo-32734: Fix asyncio.Lock multiple acquire safety issue (GH-5466) (#5502)
(cherry picked from commit d41e9e0952393e64f2f9756d778553d704191086)
Diffstat (limited to 'Lib/asyncio')
-rw-r--r-- | Lib/asyncio/locks.py | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py index 9266183..14d21ff 100644 --- a/Lib/asyncio/locks.py +++ b/Lib/asyncio/locks.py @@ -172,16 +172,22 @@ class Lock(_ContextManagerMixin): fut = self._loop.create_future() self._waiters.append(fut) + + # Finally block should be called before the CancelledError + # handling as we don't want CancelledError to call + # _wake_up_first() and attempt to wake up itself. try: - yield from fut - self._locked = True - return True + try: + yield from fut + finally: + self._waiters.remove(fut) except futures.CancelledError: if not self._locked: self._wake_up_first() raise - finally: - self._waiters.remove(fut) + + self._locked = True + return True def release(self): """Release a lock. @@ -201,11 +207,17 @@ class Lock(_ContextManagerMixin): raise RuntimeError('Lock is not acquired.') def _wake_up_first(self): - """Wake up the first waiter who isn't cancelled.""" - for fut in self._waiters: - if not fut.done(): - fut.set_result(True) - break + """Wake up the first waiter if it isn't done.""" + try: + fut = next(iter(self._waiters)) + except StopIteration: + return + + # .done() necessarily means that a waiter will wake up later on and + # either take the lock, or, if it was cancelled and lock wasn't + # taken already, will hit this again and wake up a new waiter. + if not fut.done(): + fut.set_result(True) class Event: |