gh-135736: Fix asyncio.TaskGroup swallowing errors on GeneratorExit#154538
Open
sreehariannam wants to merge 1 commit into
Open
gh-135736: Fix asyncio.TaskGroup swallowing errors on GeneratorExit#154538sreehariannam wants to merge 1 commit into
sreehariannam wants to merge 1 commit into
Conversation
sreehariannam
requested review from
1st1,
asvetlov,
kumaraditya303 and
willingc
as code owners
July 23, 2026 12:31
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.