]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.8] bpo-32751: Wait for task cancel in asyncio.wait_for() when timeout <= 0 (GH...
authorElvis Pranskevichus <elvis@magic.io>
Wed, 26 Aug 2020 20:59:17 +0000 (13:59 -0700)
committerGitHub <noreply@github.com>
Wed, 26 Aug 2020 20:59:17 +0000 (13:59 -0700)
When I was fixing bpo-32751 back in GH-7216 I missed the case when
*timeout* is zero or negative.  This takes care of that.

Props to @aaliddell for noticing the inconsistency..
(cherry picked from commit c517fc712105c8e5930cb42baaebdbe37fc3e15f)

Lib/asyncio/tasks.py
Lib/test/test_asyncio/test_tasks.py
Misc/NEWS.d/next/Library/2020-08-15-15-50-12.bpo-32751.85je5X.rst [new file with mode: 0644]

index 373be37fb4bdd60fcb8ed95a7593f6c01b872dac..9ca9fa0a94a938c108ad00855355ea2c8bc9560d 100644 (file)
@@ -460,8 +460,13 @@ async def wait_for(fut, timeout, *, loop=None):
         if fut.done():
             return fut.result()
 
-        fut.cancel()
-        raise exceptions.TimeoutError()
+        await _cancel_and_wait(fut, loop=loop)
+        try:
+            fut.result()
+        except exceptions.CancelledError as exc:
+            raise exceptions.TimeoutError() from exc
+        else:
+            raise exceptions.TimeoutError()
 
     waiter = loop.create_future()
     timeout_handle = loop.call_later(timeout, _release_waiter, waiter)
index a70dc0a87e1c4939e0704c04997813c60f900b61..c402f8f021f3a25c944c12943392ca857bfd2c47 100644 (file)
@@ -888,6 +888,9 @@ class BaseTaskTests:
                 nonlocal task_done
                 try:
                     await asyncio.sleep(0.2)
+                except asyncio.CancelledError:
+                    await asyncio.sleep(0.1)
+                    raise
                 finally:
                     task_done = True
 
@@ -900,6 +903,34 @@ class BaseTaskTests:
 
         loop.run_until_complete(foo())
 
+    def test_wait_for_waits_for_task_cancellation_w_timeout_0(self):
+        loop = asyncio.new_event_loop()
+        self.addCleanup(loop.close)
+
+        task_done = False
+
+        async def foo():
+            async def inner():
+                nonlocal task_done
+                try:
+                    await asyncio.sleep(10)
+                except asyncio.CancelledError:
+                    await asyncio.sleep(0.1)
+                    raise
+                finally:
+                    task_done = True
+
+            inner_task = self.new_task(loop, inner())
+            await asyncio.sleep(0.1)
+            await asyncio.wait_for(inner_task, timeout=0)
+
+        with self.assertRaises(asyncio.TimeoutError) as cm:
+            loop.run_until_complete(foo())
+
+        self.assertTrue(task_done)
+        chained = cm.exception.__context__
+        self.assertEqual(type(chained), asyncio.CancelledError)
+
     def test_wait_for_self_cancellation(self):
         loop = asyncio.new_event_loop()
         self.addCleanup(loop.close)
diff --git a/Misc/NEWS.d/next/Library/2020-08-15-15-50-12.bpo-32751.85je5X.rst b/Misc/NEWS.d/next/Library/2020-08-15-15-50-12.bpo-32751.85je5X.rst
new file mode 100644 (file)
index 0000000..c172ce5
--- /dev/null
@@ -0,0 +1,3 @@
+When cancelling the task due to a timeout, :meth:`asyncio.wait_for` will now
+wait until the cancellation is complete also in the case when *timeout* is
+<= 0, like it does with positive timeouts.