Skip to content

Commit ed6934e

Browse files
gh-128588: gh-128550: remove eager tasks optimization that missed and introduced incorrect cancellations (#129063)
Co-authored-by: Kumar Aditya <[email protected]>
1 parent ab61d3f commit ed6934e

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

Lib/asyncio/taskgroups.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,12 @@ def create_task(self, coro, *, name=None, context=None):
197197
else:
198198
task = self._loop.create_task(coro, name=name, context=context)
199199

200-
# optimization: Immediately call the done callback if the task is
200+
# Always schedule the done callback even if the task is
201201
# already done (e.g. if the coro was able to complete eagerly),
202-
# and skip scheduling a done callback
203-
if task.done():
204-
self._on_task_done(task)
205-
else:
206-
self._tasks.add(task)
207-
task.add_done_callback(self._on_task_done)
202+
# otherwise if the task completes with an exception then it will cancel
203+
# the current task too early. gh-128550, gh-128588
204+
self._tasks.add(task)
205+
task.add_done_callback(self._on_task_done)
208206
try:
209207
return task
210208
finally:

Lib/test/test_asyncio/test_taskgroups.py

+50
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,56 @@ async def asyncfn():
10531053
self.assertEqual(name, "example name")
10541054

10551055

1056+
async def test_cancels_task_if_created_during_creation(self):
1057+
# regression test for gh-128550
1058+
ran = False
1059+
class MyError(Exception):
1060+
pass
1061+
1062+
exc = None
1063+
try:
1064+
async with asyncio.TaskGroup() as tg:
1065+
async def third_task():
1066+
raise MyError("third task failed")
1067+
1068+
async def second_task():
1069+
nonlocal ran
1070+
tg.create_task(third_task())
1071+
with self.assertRaises(asyncio.CancelledError):
1072+
await asyncio.sleep(0) # eager tasks cancel here
1073+
await asyncio.sleep(0) # lazy tasks cancel here
1074+
ran = True
1075+
1076+
tg.create_task(second_task())
1077+
except* MyError as excs:
1078+
exc = excs.exceptions[0]
1079+
1080+
self.assertTrue(ran)
1081+
self.assertIsInstance(exc, MyError)
1082+
1083+
1084+
async def test_cancellation_does_not_leak_out_of_tg(self):
1085+
class MyError(Exception):
1086+
pass
1087+
1088+
async def throw_error():
1089+
raise MyError
1090+
1091+
try:
1092+
async with asyncio.TaskGroup() as tg:
1093+
tg.create_task(throw_error())
1094+
except* MyError:
1095+
pass
1096+
else:
1097+
self.fail("should have raised one MyError in group")
1098+
1099+
# if this test fails this current task will be cancelled
1100+
# outside the task group and inside unittest internals
1101+
# we yield to the event loop with sleep(0) so that
1102+
# cancellation happens here and error is more understandable
1103+
await asyncio.sleep(0)
1104+
1105+
10561106
class TestTaskGroup(BaseTestTaskGroup, unittest.IsolatedAsyncioTestCase):
10571107
loop_factory = asyncio.EventLoop
10581108

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Removed an incorrect optimization relating to eager tasks in :class:`asyncio.TaskGroup` that resulted in cancellations being missed.

0 commit comments

Comments
 (0)