gh-135736: Fix asyncio.TaskGroup swallowing errors on GeneratorExit#154538
Open
sreehariannam wants to merge 7 commits into
Open
gh-135736: Fix asyncio.TaskGroup swallowing errors on GeneratorExit#154538sreehariannam wants to merge 7 commits into
sreehariannam wants to merge 7 commits into
Conversation
sreehariannam
requested review from
1st1,
asvetlov,
kumaraditya303 and
willingc
as code owners
July 23, 2026 12:31
| assert not self._tasks | ||
|
|
||
| if self._base_error is not None: | ||
| if isinstance(self._base_error, GeneratorExit): |
Contributor
There was a problem hiding this comment.
I think the exception handler should be called for all base errors.
Author
There was a problem hiding this comment.
Good catch, fixed — the reporting is no longer gated on isinstance(self._base_error, GeneratorExit), so suppressed sibling-task errors are now reported via call_exception_handler whenever any base error (SystemExit, KeyboardInterrupt, etc.) causes the TaskGroup to exit, not just GeneratorExit. Added test_taskgroup_20f covering the non-GeneratorExit case.
…, not just GeneratorExit Per review feedback from kumaraditya303: the exception-handler reporting of suppressed sibling-task errors was previously gated on isinstance(self._base_error, GeneratorExit), but any base error (SystemExit, KeyboardInterrupt, etc.) causes the same silent discarding of collected task errors. Broaden the reporting to run whenever self._base_error is set, and add a regression test covering a non-GeneratorExit base error.
sreehariannam
force-pushed
the
gh-135736-taskgroup-generatorexit
branch
from
July 24, 2026 15:44
331b20c to
f878aeb
Compare
… a custom BaseException TaskGroup._is_base_error() only recognizes SystemExit and KeyboardInterrupt (GeneratorExit is handled separately); an arbitrary BaseException subclass like MyBaseExc never sets self._base_error, so it takes the BaseExceptionGroup-wrapping path instead of the direct-raise path this test meant to exercise, causing CI failures across all platforms. Use KeyboardInterrupt (matching the existing test_taskgroup_20) instead.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes gh-135736.
Problem
When the body of an
async with asyncio.TaskGroup()block exits via a bareGeneratorExit(for example, because it's inside an async generator that gets closed withaclose()), theGeneratorExitwas wrapped in aBaseExceptionGrouplike any other exception. Since callers ofaclose()/athrow()only know how to handleGeneratorExit(orStopAsyncIteration) coming back out, this surfaced as a spurious, hard-to-debugExceptionGroup: unhandled errors in a TaskGroupinstead of a clean generator close.Fix
TaskGroup._aexit()now treats aGeneratorExitraised by theasync withbody itself as a "base error" (likeSystemExit/KeyboardInterrupt) and re-raises it directly instead of wrapping it, soaclose()sees the exception type it expects.This is deliberately scoped to the body-exception path only.
TaskGroup._is_base_error()itself (used separately in_on_task_done()for exceptions raised by child tasks) is untouched — a task raisingGeneratorExitinternally is not a "close this generator" signal and continues to be wrapped in the group as before (covered by a new test,test_taskgroup_20e).Since only one exception can propagate out through
GeneratorExit-closing APIs, any sibling task errors that would otherwise be silently discarded in this path are now reported vialoop.call_exception_handler()instead of disappearing (test_taskgroup_20c).Testing
Added four tests (
test_taskgroup_20b-test_taskgroup_20e) covering: bare propagation, propagation alongside a failing sibling task (checking the exception handler is invoked), a faithful reproduction of the original report via a real async generator +aclose(), and the unchanged task-level behavior. Verified each new test fails without the fix by reverting it locally and re-running. Fulltest_taskgroupssuite (126 tests) passes.