]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #22970: asyncio: Fix inconsistency cancelling Condition.wait.
authorYury Selivanov <yury@magic.io>
Sat, 11 Jun 2016 16:00:07 +0000 (12:00 -0400)
committerYury Selivanov <yury@magic.io>
Sat, 11 Jun 2016 16:00:07 +0000 (12:00 -0400)
Patch by David Coles.

Lib/asyncio/locks.py
Lib/test/test_asyncio/test_locks.py
Misc/NEWS

index 842d6210d54c7663cc88698f93ee717ffb40cabb..741aaf27c5ee65b5e8d775cf3e6a80195e690ccc 100644 (file)
@@ -329,7 +329,13 @@ class Condition(_ContextManagerMixin):
                 self._waiters.remove(fut)
 
         finally:
-            yield from self.acquire()
+            # Must reacquire lock even if wait is cancelled
+            while True:
+                try:
+                    yield from self.acquire()
+                    break
+                except futures.CancelledError:
+                    pass
 
     @coroutine
     def wait_for(self, predicate):
index cdf5d9d3b6352606c2b870d198d0429a17e424c3..d3bdc513851b74044b4f3de36cf672dc1733a058 100644 (file)
@@ -457,6 +457,31 @@ class ConditionTests(test_utils.TestCase):
         self.assertFalse(cond._waiters)
         self.assertTrue(cond.locked())
 
+    def test_wait_cancel_contested(self):
+        cond = asyncio.Condition(loop=self.loop)
+
+        self.loop.run_until_complete(cond.acquire())
+        self.assertTrue(cond.locked())
+
+        wait_task = asyncio.Task(cond.wait(), loop=self.loop)
+        test_utils.run_briefly(self.loop)
+        self.assertFalse(cond.locked())
+
+        # Notify, but contest the lock before cancelling
+        self.loop.run_until_complete(cond.acquire())
+        self.assertTrue(cond.locked())
+        cond.notify()
+        self.loop.call_soon(wait_task.cancel)
+        self.loop.call_soon(cond.release)
+
+        try:
+            self.loop.run_until_complete(wait_task)
+        except asyncio.CancelledError:
+            # Should not happen, since no cancellation points
+            pass
+
+        self.assertTrue(cond.locked())
+
     def test_wait_unacquired(self):
         cond = asyncio.Condition(loop=self.loop)
         self.assertRaises(
index c40fc4b25c724a883087771024b04b37c9564f9e..1ed87ac6b87536207e90abd0aa9cc56b43dc1335 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -524,6 +524,9 @@ Library
   _conn_lost.
   Patch by Ćukasz Langa.
 
+- Issue #22970: asyncio: Fix inconsistency cancelling Condition.wait.
+  Patch by David Coles.
+
 IDLE
 ----