]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-37658: Fix asyncio.wait_for() to respect waited task status (#21894)
authorElvis Pranskevichus <elvis@magic.io>
Wed, 26 Aug 2020 16:42:45 +0000 (09:42 -0700)
committerGitHub <noreply@github.com>
Wed, 26 Aug 2020 16:42:45 +0000 (09:42 -0700)
Currently, if `asyncio.wait_for()` itself is cancelled it will always
raise `CancelledError` regardless if the underlying task is still
running.  This is similar to a race with the timeout, which is handled
already.

Lib/asyncio/tasks.py
Lib/test/test_asyncio/test_tasks.py
Misc/NEWS.d/next/Library/2020-08-15-15-21-40.bpo-37658.f9nivB.rst [new file with mode: 0644]

index 7ecec9638489df293fd57de0655ba05e5fd87608..8b05434f273b52ef9a2ed1c1304c56676c2ba598 100644 (file)
@@ -465,9 +465,12 @@ async def wait_for(fut, timeout, *, loop=None):
         try:
             await waiter
         except exceptions.CancelledError:
-            fut.remove_done_callback(cb)
-            fut.cancel()
-            raise
+            if fut.done():
+                return fut.result()
+            else:
+                fut.remove_done_callback(cb)
+                fut.cancel()
+                raise
 
         if fut.done():
             return fut.result()
index 511961c32005a0e42cb8e8415c752df3109eeb4f..74fc1e4a42133c30c5123d969bbcff2da2cd293a 100644 (file)
@@ -1120,6 +1120,22 @@ class BaseTaskTests:
         res = loop.run_until_complete(task)
         self.assertEqual(res, "ok")
 
+    def test_wait_for_cancellation_race_condition(self):
+        def gen():
+            yield 0.1
+            yield 0.1
+            yield 0.1
+            yield 0.1
+
+        loop = self.new_test_loop(gen)
+
+        fut = self.new_future(loop)
+        loop.call_later(0.1, fut.set_result, "ok")
+        task = loop.create_task(asyncio.wait_for(fut, timeout=1))
+        loop.call_later(0.1, task.cancel)
+        res = loop.run_until_complete(task)
+        self.assertEqual(res, "ok")
+
     def test_wait_for_waits_for_task_cancellation(self):
         loop = asyncio.new_event_loop()
         self.addCleanup(loop.close)
diff --git a/Misc/NEWS.d/next/Library/2020-08-15-15-21-40.bpo-37658.f9nivB.rst b/Misc/NEWS.d/next/Library/2020-08-15-15-21-40.bpo-37658.f9nivB.rst
new file mode 100644 (file)
index 0000000..694fbbb
--- /dev/null
@@ -0,0 +1,2 @@
+:meth:`asyncio.wait_for` now properly handles races between cancellation of
+itself and the completion of the wrapped awaitable.