Skip to content

gh-95097: support custom tasks with missing uncancel methods #95098

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
wants to merge 3 commits into from
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
2 changes: 1 addition & 1 deletion Lib/asyncio/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def run(self, coro, *, context=None):
events.set_event_loop(self._loop)
return self._loop.run_until_complete(task)
except exceptions.CancelledError:
if self._interrupt_count > 0 and task.uncancel() == 0:
if self._interrupt_count > 0 and tasks._uncancel(task) == 0:
raise KeyboardInterrupt()
else:
raise # CancelledError
Expand Down
2 changes: 1 addition & 1 deletion Lib/asyncio/taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async def __aexit__(self, et, exc, tb):

if et is not None:
if et is exceptions.CancelledError:
if self._parent_cancel_requested and not self._parent_task.uncancel():
if self._parent_cancel_requested and tasks._uncancel(self._parent_task) == 0:
# Do nothing, i.e. swallow the error.
pass
else:
Expand Down
22 changes: 19 additions & 3 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,30 @@ def _set_task_name(task, name):
try:
set_name = task.set_name
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a task is missing set_name it's probably missing uncancel

except AttributeError:
warnings.warn("Task.set_name() was added in Python 3.8, "
warnings._deprecated(
name="missing asyncio.Task.set_name()",
message="Task.set_name() was added in Python 3.8, "
"the method support will be mandatory for third-party "
"task implementations since 3.13.",
DeprecationWarning, stacklevel=3)
"task implementations since 3.13.", remove=(3, 13))
else:
set_name(name)


def _uncancel(task):
try:
uncancel = task.uncancel
except AttributeError:
warnings._deprecated(
name="missing asyncio.Task.uncancel()",
message="Task.uncancel() was added in Python 3.11, "
"the method support will be mandatory for third-party "
"task implementations since 3.16.",
remove=(3, 16))
return -1
else:
return uncancel()


class Task(futures._PyFuture): # Inherit Python Task implementation
# from a Python Future implementation.

Expand Down
2 changes: 1 addition & 1 deletion Lib/asyncio/timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ async def __aexit__(
if self._state is _State.EXPIRING:
self._state = _State.EXPIRED

if self._task.uncancel() == 0 and exc_type is exceptions.CancelledError:
if tasks._uncancel(self._task) == 0 and exc_type is exceptions.CancelledError:
# Since there are no outstanding cancel requests, we're
# handling this.
raise TimeoutError
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
support custom tasks with missing uncancel methods