Skip to content

Define our own TaskGroup subtype that doesn't munge up GeneratorExit#181

Open
msullivan wants to merge 1 commit into
mainfrom
taskgroup
Open

Define our own TaskGroup subtype that doesn't munge up GeneratorExit#181
msullivan wants to merge 1 commit into
mainfrom
taskgroup

Conversation

@msullivan

Copy link
Copy Markdown
Contributor

TaskGroup will wrap a GeneratorExit in the with body into an
ExceptionGroup, which then won't be recognized right by the enclosing
generator's machinery.

Fix this by unwrapping an ExceptionGroup if it contains only a
GeneratorExit that was raised by the context body. This means that the
straightforward case still works cleanly but we don't ever throw away
exceptions from tasks.

Previously we were handling this case with a second decorator, just in
merge, but I've been seeing this error in other places hit logs
while working on seal.

This is a known issue in Python but there doesn't seem to quite have
been consensus on what the fix is?

This not quite what Yury proposed in
python/cpython#135736 (comment),
which is to always propagate GeneratorExit directly, and to call
loop.call_exception_handler() if there are any exceptions in tasks.

I prefer the version in this PR because it keeps the task errors
reported while still covering the common case, but I could be missing
something. @1st1?

If we like this approach then maybe we should suggest it as the fix
for the cpython issue?
(There might also be some reason why it is suitable for our use but
not as the default?)

TaskGroup will wrap a GeneratorExit in the with body into an
ExceptionGroup, which then won't be recognized right by the enclosing
generator's machinery.

Fix this by unwrapping an ExceptionGroup if it contains *only* a
GeneratorExit that was raised by the context body. This means that the
straightforward case still works cleanly but we don't ever throw away
exceptions from tasks.

Previously we were handling this case with a second decorator, just in
`merge`, but I've been seeing this error in other places hit logs
while working on seal.

This is a known issue in Python but there doesn't seem to quite have
been consensus on what the fix is?

This not quite what Yury proposed in
python/cpython#135736 (comment),
which is to *always* propagate GeneratorExit directly, and to call
`loop.call_exception_handler()` if there are any exceptions in tasks.

I prefer the version in this PR because it keeps the task errors
reported while still covering the common case, but I could be missing
something. @1st1?

If we like this approach then maybe we should suggest it as the fix
for the cpython issue?
(There might also be some reason why it is suitable for our use but
not as the default?)
@msullivan msullivan requested review from 1st1 and anbuzin June 18, 2026 18:36
@vercel

vercel Bot commented Jun 18, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
ai-python Ready Ready Preview, Comment Jun 18, 2026 6:38pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants