From ed6934e71e55d398df8263f4697f58e4a3815f69 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Mon, 20 Jan 2025 17:13:01 +0000 Subject: gh-128588: gh-128550: remove eager tasks optimization that missed and introduced incorrect cancellations (#129063) Co-authored-by: Kumar Aditya --- Lib/asyncio/taskgroups.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'Lib/asyncio/taskgroups.py') diff --git a/Lib/asyncio/taskgroups.py b/Lib/asyncio/taskgroups.py index 8af199d6dcc..8fda6c8d55e 100644 --- a/Lib/asyncio/taskgroups.py +++ b/Lib/asyncio/taskgroups.py @@ -197,14 +197,12 @@ class TaskGroup: else: task = self._loop.create_task(coro, name=name, context=context) - # optimization: Immediately call the done callback if the task is + # Always schedule the done callback even if the task is # already done (e.g. if the coro was able to complete eagerly), - # and skip scheduling a done callback - if task.done(): - self._on_task_done(task) - else: - self._tasks.add(task) - task.add_done_callback(self._on_task_done) + # otherwise if the task completes with an exception then it will cancel + # the current task too early. gh-128550, gh-128588 + self._tasks.add(task) + task.add_done_callback(self._on_task_done) try: return task finally: -- cgit v1.2.3