]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-135736: Fix interaction between TaskGroup and aclose() (#154649)
authorMichael J. Sullivan <sully@msully.net>
Wed, 29 Jul 2026 21:08:45 +0000 (14:08 -0700)
committerGitHub <noreply@github.com>
Wed, 29 Jul 2026 21:08:45 +0000 (14:08 -0700)
commit49f96670d98c50eca769dd3f6fb5b6de11010617
treea0edc4373635d1cf741299f8ef63440d15a1feef
parent11d0da5b54b1a162e8f2566675007cfb2db797b5
gh-135736: Fix interaction between TaskGroup and aclose() (#154649)

Currently if you have a generator like:
```
async def test():
    async with asyncio.TaskGroup() as tg:
        async for x in whatever():
            yield x
```

If aclose() is called on the generator and GeneratorExit is raised,
the TaskGroup's __aexit__ will raise a BaseExceptionGroup, and that
will be raised from the aclose() call instead of it being swallowed.

Fix this by raising GeneratorExit from __aexit__ when:
1. The body of task group raised it
2. No subtasks raised exceptions

This makes GeneratorExit work without swallowing other exceptions
(like it would if we just added it to _is_base_error).
Doc/library/asyncio-task.rst
Lib/asyncio/taskgroups.py
Lib/test/test_asyncio/test_taskgroups.py
Misc/NEWS.d/next/Library/2026-07-24-12-29-15.gh-issue-135736.DyvA2m.rst [new file with mode: 0644]