Kaydet (Commit) 5746510b authored tarafından Bar Harel's avatar Bar Harel Kaydeden (comit) Andrew Svetlov

bpo-32841: Fix cancellation in awaiting asyncio.Condition (#5665)

üst 3384d38d
...@@ -358,12 +358,16 @@ class Condition(_ContextManagerMixin): ...@@ -358,12 +358,16 @@ class Condition(_ContextManagerMixin):
finally: finally:
# Must reacquire lock even if wait is cancelled # Must reacquire lock even if wait is cancelled
cancelled = False
while True: while True:
try: try:
await self.acquire() await self.acquire()
break break
except futures.CancelledError: except futures.CancelledError:
pass cancelled = True
if cancelled:
raise futures.CancelledError
async def wait_for(self, predicate): async def wait_for(self, predicate):
"""Wait until a predicate becomes true. """Wait until a predicate becomes true.
......
...@@ -248,8 +248,6 @@ class LockTests(test_utils.TestCase): ...@@ -248,8 +248,6 @@ class LockTests(test_utils.TestCase):
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertTrue(t3.cancelled()) self.assertTrue(t3.cancelled())
def test_finished_waiter_cancelled(self): def test_finished_waiter_cancelled(self):
lock = asyncio.Lock(loop=self.loop) lock = asyncio.Lock(loop=self.loop)
...@@ -576,6 +574,31 @@ class ConditionTests(test_utils.TestCase): ...@@ -576,6 +574,31 @@ class ConditionTests(test_utils.TestCase):
self.assertTrue(cond.locked()) self.assertTrue(cond.locked())
def test_wait_cancel_after_notify(self):
# See bpo-32841
cond = asyncio.Condition(loop=self.loop)
waited = False
async def wait_on_cond():
nonlocal waited
async with cond:
waited = True # Make sure this area was reached
await cond.wait()
waiter = asyncio.ensure_future(wait_on_cond(), loop=self.loop)
test_utils.run_briefly(self.loop) # Start waiting
self.loop.run_until_complete(cond.acquire())
cond.notify()
test_utils.run_briefly(self.loop) # Get to acquire()
waiter.cancel()
test_utils.run_briefly(self.loop) # Activate cancellation
cond.release()
test_utils.run_briefly(self.loop) # Cancellation should occur
self.assertTrue(waiter.cancelled())
self.assertTrue(waited)
def test_wait_unacquired(self): def test_wait_unacquired(self):
cond = asyncio.Condition(loop=self.loop) cond = asyncio.Condition(loop=self.loop)
self.assertRaises( self.assertRaises(
......
Fixed `asyncio.Condition` issue which silently ignored cancellation after
notifying and cancelling a conditional lock. Patch by Bar Harel.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment