Skip to content

gh-155418: Fix TaskGroup hang when a task cancels it before suspending - #155421

Open
deadlovelll wants to merge 3 commits into
python:mainfrom
deadlovelll:gh-155418-tg-hang
Open

gh-155418: Fix TaskGroup hang when a task cancels it before suspending#155421
deadlovelll wants to merge 3 commits into
python:mainfrom
deadlovelll:gh-155418-tg-hang

Conversation

@deadlovelll

Copy link
Copy Markdown
Contributor

Fix TaskGroup hang when a task cancels it before suspending

For more details see gh-155418

await asyncio.sleep(10)

async with asyncio.TaskGroup() as tg:
task = tg.create_task(child(tg))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use your reproducer with the print as well? could be useful to catch (instead of print, just use a list and mutate it)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! just pushed the changes

tg.cancel()
await asyncio.sleep(10)

async with asyncio.TaskGroup() as tg:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the problem was with the eager task factory?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi! if i understand you well - BaseTestTaskGroup class that stores all of the tests is not inheriting from unittests TestCase https://github.com/deadlovelll/cpython/blob/62ab3f87bdb17aafa97e44fd26386893ead0497b/Lib/test/test_asyncio/test_taskgroups.py#L60.

Runners are TestTaskGroup and TestEagerTaskTaskGroup that inherits from BaseTestTaskGroup and unittest.IsolatedAsyncioTestCase, they are located at the bottom of the file https://github.com/deadlovelll/cpython/blob/62ab3f87bdb17aafa97e44fd26386893ead0497b/Lib/test/test_asyncio/test_taskgroups.py#L1307-L1315

TestEagerTaskTaskGroup already inject eager factory for this

async def child(tg):
tg.cancel()
await asyncio.sleep(10)
done.append(True)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just do self.fail(<message>)

@@ -0,0 +1,2 @@
Fix :class:`asyncio.TaskGroup` hang when a task cancels it before

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should mention about eager task factory

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comments, I'll fix them during the day

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pushed the changes, take a look please

@kumaraditya303 kumaraditya303 added topic-asyncio needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes topic-asyncio

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants