bpo-38248: Fix inconsistent immediate asyncio.Task cancellation (GH-16330)

This commit is contained in:
Yury Selivanov 2019-09-25 03:32:08 -07:00 committed by Carol Willing
parent c64af8fad3
commit edad4d89e3
4 changed files with 14 additions and 8 deletions

View File

@ -284,7 +284,7 @@ class Task(futures._PyFuture): # Inherit Python Task implementation
if self._must_cancel: if self._must_cancel:
# Task is cancelled right before coro stops. # Task is cancelled right before coro stops.
self._must_cancel = False self._must_cancel = False
super().set_exception(exceptions.CancelledError()) super().cancel()
else: else:
super().set_result(exc.value) super().set_result(exc.value)
except exceptions.CancelledError: except exceptions.CancelledError:

View File

@ -604,9 +604,11 @@ class BaseTaskTests:
return 12 return 12
t = self.new_task(loop, task()) t = self.new_task(loop, task())
self.assertFalse(t.cancelled())
self.assertRaises( self.assertRaises(
asyncio.CancelledError, loop.run_until_complete, t) asyncio.CancelledError, loop.run_until_complete, t)
self.assertTrue(t.done()) self.assertTrue(t.done())
self.assertTrue(t.cancelled())
self.assertFalse(t._must_cancel) # White-box test. self.assertFalse(t._must_cancel) # White-box test.
self.assertFalse(t.cancel()) self.assertFalse(t.cancel())
@ -621,9 +623,11 @@ class BaseTaskTests:
return 12 return 12
t = self.new_task(loop, task()) t = self.new_task(loop, task())
self.assertFalse(t.cancelled())
self.assertRaises( self.assertRaises(
asyncio.CancelledError, loop.run_until_complete, t) asyncio.CancelledError, loop.run_until_complete, t)
self.assertTrue(t.done()) self.assertTrue(t.done())
self.assertTrue(t.cancelled())
self.assertFalse(t._must_cancel) # White-box test. self.assertFalse(t._must_cancel) # White-box test.
self.assertFalse(t.cancel()) self.assertFalse(t.cancel())

View File

@ -0,0 +1 @@
asyncio: Fix inconsistent immediate Task cancellation

View File

@ -2628,18 +2628,19 @@ task_step_impl(TaskObj *task, PyObject *exc)
if (_PyGen_FetchStopIterationValue(&o) == 0) { if (_PyGen_FetchStopIterationValue(&o) == 0) {
/* The error is StopIteration and that means that /* The error is StopIteration and that means that
the underlying coroutine has resolved */ the underlying coroutine has resolved */
PyObject *res;
if (task->task_must_cancel) { if (task->task_must_cancel) {
// Task is cancelled right before coro stops. // Task is cancelled right before coro stops.
Py_DECREF(o);
task->task_must_cancel = 0; task->task_must_cancel = 0;
et = asyncio_CancelledError; res = future_cancel((FutureObj*)task);
Py_INCREF(et);
ev = NULL;
tb = NULL;
goto set_exception;
} }
PyObject *res = future_set_result((FutureObj*)task, o); else {
res = future_set_result((FutureObj*)task, o);
}
Py_DECREF(o); Py_DECREF(o);
if (res == NULL) { if (res == NULL) {
return NULL; return NULL;
} }