Skip to content

[3.11] GH-95097: fix asyncio.run for tasks without uncancel method (GH-95211) #95387

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
Jul 29, 2022
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
9 changes: 5 additions & 4 deletions Lib/asyncio/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,11 @@ 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:
raise KeyboardInterrupt()
else:
raise # CancelledError
if self._interrupt_count > 0:
uncancel = getattr(task, "uncancel", None)
if uncancel is not None and uncancel() == 0:
raise KeyboardInterrupt()
raise # CancelledError
finally:
if (sigint_handler is not None
and signal.getsignal(signal.SIGINT) is sigint_handler
Expand Down
51 changes: 49 additions & 2 deletions Lib/test/test_asyncio/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import signal
import threading
import unittest

from test.test_asyncio import utils as test_utils
from unittest import mock
from unittest.mock import patch
from test.test_asyncio import utils as test_utils


def tearDownModule():
Expand Down Expand Up @@ -211,6 +210,54 @@ async def main():
asyncio.run(main())
self.assertTrue(policy.set_event_loop.called)

def test_asyncio_run_without_uncancel(self):
# See https://github.com/python/cpython/issues/95097
class Task:
def __init__(self, loop, coro, **kwargs):
self._task = asyncio.Task(coro, loop=loop, **kwargs)

def cancel(self, *args, **kwargs):
return self._task.cancel(*args, **kwargs)

def add_done_callback(self, *args, **kwargs):
return self._task.add_done_callback(*args, **kwargs)

def remove_done_callback(self, *args, **kwargs):
return self._task.remove_done_callback(*args, **kwargs)

@property
def _asyncio_future_blocking(self):
return self._task._asyncio_future_blocking

def result(self, *args, **kwargs):
return self._task.result(*args, **kwargs)

def done(self, *args, **kwargs):
return self._task.done(*args, **kwargs)

def cancelled(self, *args, **kwargs):
return self._task.cancelled(*args, **kwargs)

def exception(self, *args, **kwargs):
return self._task.exception(*args, **kwargs)

def get_loop(self, *args, **kwargs):
return self._task.get_loop(*args, **kwargs)


async def main():
interrupt_self()
await asyncio.Event().wait()

def new_event_loop():
loop = self.new_loop()
loop.set_task_factory(Task)
return loop

asyncio.set_event_loop_policy(TestPolicy(new_event_loop))
with self.assertRaises(asyncio.CancelledError):
asyncio.run(main())


class RunnerTests(BaseTest):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :func:`asyncio.run` for :class:`asyncio.Task` implementations without :meth:`~asyncio.Task.uncancel` method. Patch by Kumar Aditya.