Fix waiter cancellation in asyncio.Lock (#1031) (#2037)

Avoid a deadlock when the waiter who is about to take the lock is
cancelled

Issue #27585
This commit is contained in:
Yury Selivanov 2017-06-09 17:07:48 -04:00 committed by GitHub
parent e89f95bfd0
commit fa7f519113
3 changed files with 37 additions and 5 deletions

View File

@ -176,6 +176,10 @@ class Lock(_ContextManagerMixin):
yield from fut yield from fut
self._locked = True self._locked = True
return True return True
except futures.CancelledError:
if not self._locked:
self._wake_up_first()
raise
finally: finally:
self._waiters.remove(fut) self._waiters.remove(fut)
@ -192,13 +196,16 @@ class Lock(_ContextManagerMixin):
""" """
if self._locked: if self._locked:
self._locked = False self._locked = False
# Wake up the first waiter who isn't cancelled. self._wake_up_first()
else:
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: for fut in self._waiters:
if not fut.done(): if not fut.done():
fut.set_result(True) fut.set_result(True)
break break
else:
raise RuntimeError('Lock is not acquired.')
class Event: class Event:

View File

@ -176,6 +176,28 @@ class LockTests(test_utils.TestCase):
self.assertTrue(tb.cancelled()) self.assertTrue(tb.cancelled())
self.assertTrue(tc.done()) self.assertTrue(tc.done())
def test_finished_waiter_cancelled(self):
lock = asyncio.Lock(loop=self.loop)
ta = asyncio.Task(lock.acquire(), loop=self.loop)
test_utils.run_briefly(self.loop)
self.assertTrue(lock.locked())
tb = asyncio.Task(lock.acquire(), loop=self.loop)
test_utils.run_briefly(self.loop)
self.assertEqual(len(lock._waiters), 1)
# Create a second waiter, wake up the first, and cancel it.
# Without the fix, the second was not woken up.
tc = asyncio.Task(lock.acquire(), loop=self.loop)
lock.release()
tb.cancel()
test_utils.run_briefly(self.loop)
self.assertTrue(lock.locked())
self.assertTrue(ta.done())
self.assertTrue(tb.cancelled())
def test_release_not_acquired(self): def test_release_not_acquired(self):
lock = asyncio.Lock(loop=self.loop) lock = asyncio.Lock(loop=self.loop)

View File

@ -49,6 +49,9 @@ Core and Builtins
Library Library
------- -------
- bpo-27585: Fix waiter cancellation in asyncio.Lock.
Patch by Mathieu Sornay.
- bpo-30418: On Windows, subprocess.Popen.communicate() now also ignore EINVAL - bpo-30418: On Windows, subprocess.Popen.communicate() now also ignore EINVAL
on stdin.write() if the child process is still running but closed the pipe. on stdin.write() if the child process is still running but closed the pipe.