Skip to content

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

Merged
kumaraditya303 merged 5 commits into
python:mainfrom
deadlovelll:gh-155418-tg-hang
Aug 31, 2026
Merged

gh-155418: Fix TaskGroup hang when a task cancels it before suspending#155421
kumaraditya303 merged 5 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
@kumaraditya303

Copy link
Copy Markdown
Contributor

There are merge conflicts.

@kumaraditya303
kumaraditya303 enabled auto-merge (squash) August 31, 2026 17:57
@kumaraditya303
kumaraditya303 merged commit 8e92bf5 into python:main Aug 31, 2026
51 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @deadlovelll for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @deadlovelll and @kumaraditya303, I could not cleanly backport this to 3.15 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 8e92bf54e7ae55497a1b193b875d07e10e52d0e1 3.15

@miss-islington-app

Copy link
Copy Markdown

Sorry, @deadlovelll and @kumaraditya303, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 8e92bf54e7ae55497a1b193b875d07e10e52d0e1 3.14

@deadlovelll

Copy link
Copy Markdown
Contributor Author

ugh, @kumaraditya303 i'll create the bp, if you dont mind

@deadlovelll

Copy link
Copy Markdown
Contributor Author

@kumaraditya303 there's no cancel() method on 3.14, so for this version is no need in bp

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants