[Python-checkins] bpo-38248: Fix inconsistent immediate asyncio.Task cancellation (GH-16330) (GH-16383)

Carol Willing webhook-mailer at python.org
Wed Sep 25 07:49:05 EDT 2019


https://github.com/python/cpython/commit/16cec136b75daf438080a5b6685d2679dfa406af
commit: 16cec136b75daf438080a5b6685d2679dfa406af
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Carol Willing <carolcode at willingconsulting.com>
date: 2019年09月25日T04:48:52-07:00
summary:
bpo-38248: Fix inconsistent immediate asyncio.Task cancellation (GH-16330) (GH-16383)
(cherry picked from commit edad4d89e357c92f70c0324b937845d652b20afd)
Co-authored-by: Yury Selivanov <yury at edgedb.com>
files:
A Misc/NEWS.d/next/Library/2019-09-22-13-05-36.bpo-38248.Yo3N_1.rst
M Lib/asyncio/tasks.py
M Lib/test/test_asyncio/test_tasks.py
M Modules/_asynciomodule.c
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index a0cb884eacab..38d982716d46 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -284,7 +284,7 @@ def __step(self, exc=None):
 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:
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
index 6e832eab6d58..c21b06938fe3 100644
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -604,9 +604,11 @@ def test_cancel_current_task(self):
 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 @@ def test_cancel_at_end(self):
 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
index 000000000000..fc92209af387
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-09-22-13-05-36.bpo-38248.Yo3N_1.rst
@@ -0,0 +1 @@
+asyncio: Fix inconsistent immediate Task cancellation
diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c
index 281161b68611..cea3affe99ec 100644
--- a/Modules/_asynciomodule.c
+++ b/Modules/_asynciomodule.c
@@ -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;
 }


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /