Skip to content

bpo-46771: Implement task cancel requests #31508

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

Closed
Closed
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
16 changes: 7 additions & 9 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def __init__(self, coro, *, loop=None, name=None):
else:
self._name = str(name)

self._cancel_requested = False
self._num_cancels_requested = 0
self._must_cancel = False
self._fut_waiter = None
self._coro = coro
Expand Down Expand Up @@ -202,9 +202,9 @@ def cancel(self, msg=None):
self._log_traceback = False
if self.done():
return False
if self._cancel_requested:
self._num_cancels_requested += 1
if self._num_cancels_requested > 1:
return False
self._cancel_requested = True
if self._fut_waiter is not None:
if self._fut_waiter.cancel(msg=msg):
# Leave self._fut_waiter; it may be a Task that
Expand All @@ -217,14 +217,12 @@ def cancel(self, msg=None):
return True

def cancelling(self):
return self._cancel_requested
return self._num_cancels_requested

def uncancel(self):
if self._cancel_requested:
self._cancel_requested = False
return True
else:
return False
if self._num_cancels_requested > 0:
self._num_cancels_requested -= 1
return self._num_cancels_requested

def __step(self, exc=None):
if self.done():
Expand Down
24 changes: 8 additions & 16 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ typedef struct {
PyObject *task_context;
int task_must_cancel;
int task_log_destroy_pending;
int task_cancel_requested;
int task_num_cancels_requested;
} TaskObj;

typedef struct {
Expand Down Expand Up @@ -2040,7 +2040,7 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop,
Py_CLEAR(self->task_fut_waiter);
self->task_must_cancel = 0;
self->task_log_destroy_pending = 1;
self->task_cancel_requested = 0;
self->task_num_cancels_requested = 0;
Py_INCREF(coro);
Py_XSETREF(self->task_coro, coro);

Expand Down Expand Up @@ -2207,10 +2207,10 @@ _asyncio_Task_cancel_impl(TaskObj *self, PyObject *msg)
Py_RETURN_FALSE;
}

if (self->task_cancel_requested) {
self->task_num_cancels_requested += 1;
if (self->task_num_cancels_requested > 1) {
Py_RETURN_FALSE;
}
self->task_cancel_requested = 1;

if (self->task_fut_waiter) {
PyObject *res;
Expand Down Expand Up @@ -2256,12 +2256,7 @@ _asyncio_Task_cancelling_impl(TaskObj *self)
/*[clinic end generated code: output=803b3af96f917d7e input=c50e50f9c3ca4676]*/
/*[clinic end generated code]*/
{
if (self->task_cancel_requested) {
Py_RETURN_TRUE;
}
else {
Py_RETURN_FALSE;
}
return PyLong_FromLong(self->task_num_cancels_requested);
}

/*[clinic input]
Expand All @@ -2280,13 +2275,10 @@ _asyncio_Task_uncancel_impl(TaskObj *self)
/*[clinic end generated code: output=58184d236a817d3c input=5db95e28fcb6f7cd]*/
/*[clinic end generated code]*/
{
if (self->task_cancel_requested) {
self->task_cancel_requested = 0;
Py_RETURN_TRUE;
}
else {
Py_RETURN_FALSE;
if (self->task_num_cancels_requested > 0) {
self->task_num_cancels_requested -= 1;
}
return PyLong_FromLong(self->task_num_cancels_requested);
}

/*[clinic input]
Expand Down