Skip to content

[3.15] gh-135736: Fix interaction between TaskGroup and aclose() (GH-154649) - #154900

Open
miss-islington wants to merge 1 commit into
python:3.15from
miss-islington:backport-49f9667-3.15
Open

[3.15] gh-135736: Fix interaction between TaskGroup and aclose() (GH-154649)#154900
miss-islington wants to merge 1 commit into
python:3.15from
miss-islington:backport-49f9667-3.15

Conversation

@miss-islington

@miss-islington miss-islington commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

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).
(cherry picked from commit 49f9667)

Co-authored-by: Michael J. Sullivan sully@msully.net

…onGH-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).
(cherry picked from commit 49f9667)

Co-authored-by: Michael J. Sullivan <sully@msully.net>
@read-the-docs-community

Copy link
Copy Markdown

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.

2 participants