Skip to content

Commit

Permalink
Avoid chaining current exception when exiting task-groups on asyncio (#…
Browse files Browse the repository at this point in the history
…864)

Fixes #863.
  • Loading branch information
tapetersen authored Jan 29, 2025
1 parent a5b4e55 commit 349478d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Version history

This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.

**UNRELEASED**

- Fixed traceback formatting growing quadratically with level of ``TaskGroup``
nesting on asyncio due to exception chaining when raising ``ExceptionGroups``
in ``TaskGroup.__aexit__``
(`#863 <https://github.com/agronholm/anyio/issues/863>`_; PR by @tapetersen)

**4.8.0**

- Added **experimental** support for running functions in subinterpreters on Python
Expand Down
6 changes: 5 additions & 1 deletion src/anyio/_backends/_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,9 +764,13 @@ async def __aexit__(

self._active = False
if self._exceptions:
# The exception that got us here should already have been
# added to self._exceptions so it's ok to break exception
# chaining and avoid adding a "During handling of above..."
# for each nesting level.
raise BaseExceptionGroup(
"unhandled errors in a TaskGroup", self._exceptions
)
) from None
elif exc_val:
raise exc_val
except BaseException as exc:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -1837,3 +1837,13 @@ async def test_patched_asyncio_task(monkeypatch: MonkeyPatch) -> None:
)
async with create_task_group() as tg:
tg.start_soon(sleep, 0)


async def test_exception_groups_suppresses_exc_context() -> None:
with pytest.raises(
cast(type[ExceptionGroup[Exception]], ExceptionGroup)
) as exc_info:
async with create_task_group():
raise Exception("Error")

assert exc_info.value.__suppress_context__

0 comments on commit 349478d

Please sign in to comment.