diff options
author | Bar Harel <bzvi7919@gmail.com> | 2018-02-02 22:04:00 (GMT) |
---|---|---|
committer | Yury Selivanov <yury@magic.io> | 2018-02-02 22:04:00 (GMT) |
commit | 2f79c014931cbb23b08a7d16c534a3cc9607ae14 (patch) | |
tree | 84bb7aa654ee9a7ee792ec897726da45da8ce276 /Lib/asyncio | |
parent | 66771422d0541289d0b1287bc3c28e8b5609f6b4 (diff) | |
download | cpython-2f79c014931cbb23b08a7d16c534a3cc9607ae14.zip cpython-2f79c014931cbb23b08a7d16c534a3cc9607ae14.tar.gz cpython-2f79c014931cbb23b08a7d16c534a3cc9607ae14.tar.bz2 |
bpo-32734: Fix asyncio.Lock multiple acquire safety issue (GH-5466)
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 6193837..508a214 100644 --- a/Lib/asyncio/locks.py +++ b/Lib/asyncio/locks.py @@ -183,16 +183,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: - await fut - self._locked = True - return True + try: + await 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. @@ -212,11 +218,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: |