]> granicus.if.org Git - python/commitdiff
bpo-38248: Fix inconsistent immediate asyncio.Task cancellation (GH-16330) (GH-16383)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 25 Sep 2019 11:48:52 +0000 (04:48 -0700)
committerCarol Willing <carolcode@willingconsulting.com>
Wed, 25 Sep 2019 11:48:52 +0000 (04:48 -0700)
(cherry picked from commit edad4d89e357c92f70c0324b937845d652b20afd)

Co-authored-by: Yury Selivanov <yury@edgedb.com>
Lib/asyncio/tasks.py
Lib/test/test_asyncio/test_tasks.py
Misc/NEWS.d/next/Library/2019-09-22-13-05-36.bpo-38248.Yo3N_1.rst [new file with mode: 0644]
Modules/_asynciomodule.c

index a0cb884eacab7c9486737970fb115584e72ddd11..38d982716d46ad1b5b37c8d6b03185d0274eec31 100644 (file)
@@ -284,7 +284,7 @@ class Task(futures._PyFuture):  # Inherit Python Task implementation
             if self._must_cancel:
                 # Task is cancelled right before coro stops.
                 self._must_cancel = False
-                super().set_exception(exceptions.CancelledError())
+                super().cancel()
             else:
                 super().set_result(exc.value)
         except exceptions.CancelledError:
index 6e832eab6d583588f2a3e4d6220627d47d5bbac9..c21b06938fe3f5c568f465607607f50e6b84efe2 100644 (file)
@@ -604,9 +604,11 @@ class BaseTaskTests:
             return 12
 
         t = self.new_task(loop, task())
+        self.assertFalse(t.cancelled())
         self.assertRaises(
             asyncio.CancelledError, loop.run_until_complete, t)
         self.assertTrue(t.done())
+        self.assertTrue(t.cancelled())
         self.assertFalse(t._must_cancel)  # White-box test.
         self.assertFalse(t.cancel())
 
@@ -621,9 +623,11 @@ class BaseTaskTests:
             return 12
 
         t = self.new_task(loop, task())
+        self.assertFalse(t.cancelled())
         self.assertRaises(
             asyncio.CancelledError, loop.run_until_complete, t)
         self.assertTrue(t.done())
+        self.assertTrue(t.cancelled())
         self.assertFalse(t._must_cancel)  # White-box test.
         self.assertFalse(t.cancel())
 
diff --git a/Misc/NEWS.d/next/Library/2019-09-22-13-05-36.bpo-38248.Yo3N_1.rst b/Misc/NEWS.d/next/Library/2019-09-22-13-05-36.bpo-38248.Yo3N_1.rst
new file mode 100644 (file)
index 0000000..fc92209
--- /dev/null
@@ -0,0 +1 @@
+asyncio: Fix inconsistent immediate Task cancellation
index 281161b68611aecfa33f540e3f15c0b578e06790..cea3affe99ec30e61167cdba3ad2b69aa18692bb 100644 (file)
@@ -2635,18 +2635,19 @@ task_step_impl(TaskObj *task, PyObject *exc)
         if (_PyGen_FetchStopIterationValue(&o) == 0) {
             /* The error is StopIteration and that means that
                the underlying coroutine has resolved */
+
+            PyObject *res;
             if (task->task_must_cancel) {
                 // Task is cancelled right before coro stops.
-                Py_DECREF(o);
                 task->task_must_cancel = 0;
-                et = asyncio_CancelledError;
-                Py_INCREF(et);
-                ev = NULL;
-                tb = NULL;
-                goto set_exception;
+                res = future_cancel((FutureObj*)task);
+            }
+            else {
+                res = future_set_result((FutureObj*)task, o);
             }
-            PyObject *res = future_set_result((FutureObj*)task, o);
+
             Py_DECREF(o);
+
             if (res == NULL) {
                 return NULL;
             }