8000 bpo-38248: Fix inconsistent immediate asyncio.Task cancellation by 1st1 · Pull Request #16330 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-38248: Fix inconsistent immediate asyncio.Task cancellation #16330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,11 @@ async def task():
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())

Expand All @@ -621,9 +623,11 @@ async def task():
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())

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asyncio: Fix inconsistent immediate Task cancellation
15 changes: 8 additions & 7 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
0