Skip to content

Commit 59aff22

Browse files
pythongh-128588: pythongh-128550: remove eager tasks optimization that missed and introduced incorrect cancellations (python#129063)
Co-authored-by: Kumar Aditya <[email protected]>
1 parent 958bb1c commit 59aff22

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

Lib/asyncio/taskgroups.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,14 @@ def create_task(self, coro, *, name=None, context=None):
185185
else:
186186
task = self._loop.create_task(coro, context=context)
187187
tasks._set_task_name(task, name)
188-
# optimization: Immediately call the done callback if the task is
188+
189+
# Always schedule the done callback even if the task is
189190
# already done (e.g. if the coro was able to complete eagerly),
190-
# and skip scheduling a done callback
191-
if task.done():
192-
self._on_task_done(task)
193-
else:
194-
self._tasks.add(task)
195-
task.add_done_callback(self._on_task_done)
191+
# otherwise if the task completes with an exception then it will cancel
192+
# the current task too early. gh-128550, gh-128588
193+
194+
self._tasks.add(task)
195+
task.add_done_callback(self._on_task_done)
196196
try:
197197
return task
198198
finally:

Lib/test/test_asyncio/test_taskgroups.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,55 @@ class MyKeyboardInterrupt(KeyboardInterrupt):
956956
self.assertIsNotNone(exc)
957957
self.assertListEqual(gc.get_referrers(exc), [])
958958

959+
async def test_cancels_task_if_created_during_creation(self):
960+
# regression test for gh-128550
961+
ran = False
962+
class MyError(Exception):
963+
pass
964+
965+
exc = None
966+
try:
967+
async with asyncio.TaskGroup() as tg:
968+
async def third_task():
969+
raise MyError("third task failed")
970+
971+
async def second_task():
972+
nonlocal ran
973+
tg.create_task(third_task())
974+
with self.assertRaises(asyncio.CancelledError):
975+
await asyncio.sleep(0) # eager tasks cancel here
976+
await asyncio.sleep(0) # lazy tasks cancel here
977+
ran = True
978+
979+
tg.create_task(second_task())
980+
except* MyError as excs:
981+
exc = excs.exceptions[0]
982+
983+
self.assertTrue(ran)
984+
self.assertIsInstance(exc, MyError)
985+
986+
async def test_cancellation_does_not_leak_out_of_tg(self):
987+
class MyError(Exception):
988+
pass
989+
990+
async def throw_error():
991+
raise MyError
992+
993+
try:
994+
async with asyncio.TaskGroup() as tg:
995+
tg.create_task(throw_error())
996+
except* MyError:
997+
pass
998+
else:
999+
self.fail("should have raised one MyError in group")
1000+
1001+
# if this test fails this current task will be cancelled
1002+
# outside the task group and inside unittest internals
1003+
# we yield to the event loop with sleep(0) so that
1004+
# cancellation happens here and error is more understandable
1005+
await asyncio.sleep(0)
1006+
1007+
9591008
if sys.platform == "win32":
9601009
EventLoop = asyncio.ProactorEventLoop
9611010
else:
Lines changed: 1 addition & 0 deletions
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)