From 62ab3f87bdb17aafa97e44fd26386893ead0497b Mon Sep 17 00:00:00 2001 From: Timofey Ivankov Date: Sun, 9 Aug 2026 14:29:19 +0300 Subject: [PATCH 1/4] gh-155418: Fix TaskGroup hang when a task cancels it before suspending --- Lib/asyncio/taskgroups.py | 3 +++ Lib/test/test_asyncio/test_taskgroups.py | 10 ++++++++++ .../2026-08-09-14-27-23.gh-issue-155418.kCXUIG.rst | 2 ++ 3 files changed, 15 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-08-09-14-27-23.gh-issue-155418.kCXUIG.rst diff --git a/Lib/asyncio/taskgroups.py b/Lib/asyncio/taskgroups.py index a431b45a19489d5..1fdffcc81a88655 100644 --- a/Lib/asyncio/taskgroups.py +++ b/Lib/asyncio/taskgroups.py @@ -222,6 +222,9 @@ def create_task(self, coro, **kwargs): # the current task too early. gh-128550, gh-128588 self._tasks.add(task) task.add_done_callback(self._on_task_done) + # gh-155418: an eager task can cancel the group before joining _tasks + if self._aborting and not task.done(): + task.cancel() try: return task finally: diff --git a/Lib/test/test_asyncio/test_taskgroups.py b/Lib/test/test_asyncio/test_taskgroups.py index bc246400b83e9b2..94e5fdd1f83a42e 100644 --- a/Lib/test/test_asyncio/test_taskgroups.py +++ b/Lib/test/test_asyncio/test_taskgroups.py @@ -1154,6 +1154,16 @@ async def test_taskgroup_cancel_before_create_task(self): with self.assertRaises(RuntimeError): tg.create_task(asyncio.sleep(1)) + async def test_taskgroup_cancel_from_child_before_first_suspension(self): + # gh-155418: an eager task can cancel the group before joining _tasks + async def child(tg): + tg.cancel() + await asyncio.sleep(10) + + async with asyncio.TaskGroup() as tg: + task = tg.create_task(child(tg)) + self.assertTrue(task.cancelled()) + async def test_taskgroup_cancel_before_exception(self): async def raise_exc(parent_tg: asyncio.TaskGroup): parent_tg.cancel() diff --git a/Misc/NEWS.d/next/Library/2026-08-09-14-27-23.gh-issue-155418.kCXUIG.rst b/Misc/NEWS.d/next/Library/2026-08-09-14-27-23.gh-issue-155418.kCXUIG.rst new file mode 100644 index 000000000000000..680ebf05bf58876 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-09-14-27-23.gh-issue-155418.kCXUIG.rst @@ -0,0 +1,2 @@ +Fix :class:`asyncio.TaskGroup` hang when a task cancels it before +suspending. From dd46221f91dc5d72ae2bca714711f795792f3b7b Mon Sep 17 00:00:00 2001 From: Timofey Ivankov Date: Sun, 9 Aug 2026 16:33:45 +0300 Subject: [PATCH 2/4] assert the child did not finish --- Lib/test/test_asyncio/test_taskgroups.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_asyncio/test_taskgroups.py b/Lib/test/test_asyncio/test_taskgroups.py index 94e5fdd1f83a42e..d32d661d8dce11f 100644 --- a/Lib/test/test_asyncio/test_taskgroups.py +++ b/Lib/test/test_asyncio/test_taskgroups.py @@ -1156,13 +1156,17 @@ async def test_taskgroup_cancel_before_create_task(self): async def test_taskgroup_cancel_from_child_before_first_suspension(self): # gh-155418: an eager task can cancel the group before joining _tasks + done = [] + async def child(tg): tg.cancel() await asyncio.sleep(10) + done.append(True) async with asyncio.TaskGroup() as tg: task = tg.create_task(child(tg)) self.assertTrue(task.cancelled()) + self.assertEqual(done, []) async def test_taskgroup_cancel_before_exception(self): async def raise_exc(parent_tg: asyncio.TaskGroup): From 70f7d39abd9e60c663c82ce6d841d08a8d32a4da Mon Sep 17 00:00:00 2001 From: Timofey Ivankov Date: Thu, 20 Aug 2026 17:24:13 +0300 Subject: [PATCH 3/4] review changes --- Lib/test/test_asyncio/test_taskgroups.py | 5 +---- .../Library/2026-08-09-14-27-23.gh-issue-155418.kCXUIG.rst | 4 ++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_asyncio/test_taskgroups.py b/Lib/test/test_asyncio/test_taskgroups.py index d32d661d8dce11f..1ff342ed45c845b 100644 --- a/Lib/test/test_asyncio/test_taskgroups.py +++ b/Lib/test/test_asyncio/test_taskgroups.py @@ -1156,17 +1156,14 @@ async def test_taskgroup_cancel_before_create_task(self): async def test_taskgroup_cancel_from_child_before_first_suspension(self): # gh-155418: an eager task can cancel the group before joining _tasks - done = [] - async def child(tg): tg.cancel() await asyncio.sleep(10) - done.append(True) + self.fail("the child was not cancelled") async with asyncio.TaskGroup() as tg: task = tg.create_task(child(tg)) self.assertTrue(task.cancelled()) - self.assertEqual(done, []) async def test_taskgroup_cancel_before_exception(self): async def raise_exc(parent_tg: asyncio.TaskGroup): diff --git a/Misc/NEWS.d/next/Library/2026-08-09-14-27-23.gh-issue-155418.kCXUIG.rst b/Misc/NEWS.d/next/Library/2026-08-09-14-27-23.gh-issue-155418.kCXUIG.rst index 680ebf05bf58876..7fe30ddb1ce8b79 100644 --- a/Misc/NEWS.d/next/Library/2026-08-09-14-27-23.gh-issue-155418.kCXUIG.rst +++ b/Misc/NEWS.d/next/Library/2026-08-09-14-27-23.gh-issue-155418.kCXUIG.rst @@ -1,2 +1,2 @@ -Fix :class:`asyncio.TaskGroup` hang when a task cancels it before -suspending. +Fix :class:`asyncio.TaskGroup` hang when a task created by +:func:`asyncio.eager_task_factory` cancels the group before suspending. From 14b53c18696ad9e90175778befee4f8838052155 Mon Sep 17 00:00:00 2001 From: Timofey Ivankov Date: Mon, 31 Aug 2026 20:52:22 +0300 Subject: [PATCH 4/4] fix lint --- Lib/test/test_asyncio/test_taskgroups.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_asyncio/test_taskgroups.py b/Lib/test/test_asyncio/test_taskgroups.py index c069227dbd1664f..1515672393816b4 100644 --- a/Lib/test/test_asyncio/test_taskgroups.py +++ b/Lib/test/test_asyncio/test_taskgroups.py @@ -1197,7 +1197,7 @@ async def child(tg): async with asyncio.TaskGroup() as tg: task = tg.create_task(child(tg)) self.assertTrue(task.cancelled()) - + async def test_taskgroup_cancel_keeps_outer_cancellation(self): # gh-155433: any cancellation from outside the group must propagate. async def child():