gh-128588: gh-128550: remove eager tasks optimization that missed and introduced incorrect cancellations (#129063)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
This commit is contained in:
Thomas Grainger 2025-01-20 17:13:01 +00:00 committed by GitHub
parent ab61d3f430
commit ed6934e71e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 56 additions and 7 deletions

View file

@ -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: