Skip to content

Commit

Permalink
Hide implementation-detail nursery of start when using run(..., stric…
Browse files Browse the repository at this point in the history
…t_exception_groups=True)
  • Loading branch information
gschaffner committed Oct 30, 2023
1 parent 4044f57 commit 8d2f364
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
5 changes: 4 additions & 1 deletion newsfragments/2611.bugfix.rst
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
With ``strict_exception_groups=True``, when you ran a function in a nursery which raised an exception before calling ``task_status.started()``, it previously got wrapped twice over in ``ExceptionGroup`` in some cases. It no longer does that, and also won't wrap any ``ExceptionGroup`` raised by the function itself.
When a starting function raises before calling :func:`trio.TaskStatus.started`,
:func:`trio.Nursery.start` will no longer wrap the exception in an undocumented
:exc:`ExceptionGroup`. Previously, an :exc:`ExceptionGroup` was added when using
``trio.run(..., strict_exception_groups=True)``.
13 changes: 5 additions & 8 deletions trio/_core/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,13 +1103,6 @@ def aborted(raise_cancel: _core.RaiseCancelT) -> Abort:

popped = self._parent_task._child_nurseries.pop()
assert popped is self

# don't unnecessarily wrap an exceptiongroup in another exceptiongroup
# see https://github.com/python-trio/trio/issues/2611
if len(self._pending_excs) == 1 and isinstance(
self._pending_excs[0], BaseExceptionGroup
):
return self._pending_excs[0]
if self._pending_excs:
try:
return MultiError(
Expand Down Expand Up @@ -1218,7 +1211,11 @@ async def async_fn(arg1, arg2, *, task_status=trio.TASK_STATUS_IGNORED):
raise RuntimeError("Nursery is closed to new arrivals")
try:
self._pending_starts += 1
async with open_nursery() as old_nursery:
# `strict_exception_groups=False` prevents the implementation-detail
# nursery from inheriting `strict_exception_groups=True` from the
# `run` option, which would cause it to wrap a pre-started()
# exception in an extra ExceptionGroup. See #2611.
async with open_nursery(strict_exception_groups=False) as old_nursery:
task_status: _TaskStatus[StatusT] = _TaskStatus(old_nursery, self)
thunk = functools.partial(async_fn, task_status=task_status)
task = GLOBAL_RUN_CONTEXT.runner.spawn_impl(
Expand Down

0 comments on commit 8d2f364

Please sign in to comment.