Commit edad4d89 authored by Yury Selivanov's avatar Yury Selivanov Committed by Carol Willing

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

parent c64af8fa
......@@ -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:
......
......@@ -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())
......
asyncio: Fix inconsistent immediate Task cancellation
......@@ -2628,18 +2628,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;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment