mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
Fix waiter cancellation in asyncio.Lock (#1031)
Avoid a deadlock when the waiter who is about to take the lock is cancelled Issue #27585
This commit is contained in:
parent
f9f1ccace3
commit
894a654a9c
2 changed files with 34 additions and 5 deletions
|
@ -176,6 +176,28 @@ class LockTests(test_utils.TestCase):
|
|||
self.assertTrue(tb.cancelled())
|
||||
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):
|
||||
lock = asyncio.Lock(loop=self.loop)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue