Skip to content

Internal error with recursive tasks and cancellation #3290

Open

Description

@A5rocks

I'm not really sure the cause of this nor whether we can fix this... Related to #3289

Reproducing program:

import trio

async def sub_tn():
    async with trio.open_nursery() as tn:
        tn.start_soon(trio.sleep_forever)
        tn.start_soon(sub_tn)
        try:
            await trio.sleep_forever()
        finally:
            await trio.lowlevel.checkpoint()
    

async def main():
    async with trio.open_nursery() as tn:
        tn.start_soon(trio.sleep_forever)
        tn.start_soon(sub_tn)
        await trio.sleep(2)
        tn.cancel_scope.cancel()

trio.run(main)

When I hit Ctrl+C a bunch while it runs, I get this error:

Traceback (most recent call last):
  File "C:\Users\A5rocks\Documents\trio\src\trio\_core\_run.py", line 2875, in unrolled_run
    runner.task_exited(task, final_outcome)
    ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\A5rocks\Documents\trio\src\trio\_core\_run.py", line 2053, in task_exited
    task._parent_nursery._child_finished(task, outcome)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
  File "C:\Users\A5rocks\Documents\trio\src\trio\_core\_run.py", line 1270, in _child_finished
    reason=f"child task raised exception {outcome.error!r}",
                                         ^^^^^^^^^^^^^^^^^
RecursionError: maximum recursion depth exceeded

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\A5rocks\Documents\trio\repro.py", line 23, in <module>
    trio.run(main)
    ~~~~~~~~^^^^^^
  File "C:\Users\A5rocks\Documents\trio\src\trio\_core\_run.py", line 2518, in run
    timeout = gen.send(next_send)
  File "C:\Users\A5rocks\Documents\trio\src\trio\_core\_run.py", line 2931, in unrolled_run
    raise TrioInternalError("internal error in Trio - please file a bug!") from exc
trio.TrioInternalError: internal error in Trio - please file a bug!

Activity

  1. goodboy commented on Jun 19, 2025

    @goodboy
    Member

    No credit to the original author of this ehh

    ;p

    Glad to see you were able to reproduce it!

  2. Arham-Begani commented on Oct 19, 2026

    @Arham-Begani

    I am investigating the exception-formatting failure in this traceback. A deterministic variant that raises a deeply nested ExceptionGroup from a child task reaches the same unguarded repr in the nursery cancellation path and produces TrioInternalError.

    I plan to guard diagnostic exception formatting in both child-task and nursery-body failure paths, preserving the original exception and ensuring siblings are cancelled and nursery cleanup completes. I will add deterministic regression tests for recursive ExceptionGroup repr and exceptions whose repr raises, and keep the nursery-depth design discussion in #3289 separate.

    This investigation and the proposed patch are being prepared by an automated AI coding agent on behalf of Arham-Begani. I checked the contribution guide and found no assignment requirement or separate AI-use restriction; please flag any additional applicable policy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions