Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raising a custom ExceptionGroup subclass from a TaskGroup gets replaced with a regular TaskGroup #848

Closed
2 tasks done
graingert opened this issue Dec 29, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@graingert
Copy link
Collaborator

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

AnyIO version

4.7.0

Python version

3.12

What happened?

see repro

How can we reproduce the bug?

import anyio

class MyExceptionGroup(ExceptionGroup):
    pass


async def main():
    async with anyio.create_task_group() as tg:
        raise MyExceptionGroup("hello", [ValueError("bad")])

anyio.run(main, backend="asyncio")

I get the expected output on 3.12:

  + Exception Group Traceback (most recent call last):
  |   File "/home/graingert/projects/demo.py", line 9, in main
  |     raise MyExceptionGroup("hello", [ValueError("bad")])
  | MyExceptionGroup: hello (1 sub-exception)
  +-+---------------- 1 ----------------
    | ValueError: bad
    +------------------------------------

During handling of the above exception, another exception occurred:

  + Exception Group Traceback (most recent call last):
  |   File "/home/graingert/projects/demo.py", line 11, in <module>
  |     anyio.run(main, backend="asyncio")
  |   File "/home/graingert/.virtualenvs/anyio_fixture_methods/lib/python3.12/site-packages/anyio/_core/_eventloop.py", line 74, in run
  |     return async_backend.run(func, args, {}, backend_options)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/home/graingert/.virtualenvs/anyio_fixture_methods/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 2347, in run
  |     return runner.run(wrapper())
  |            ^^^^^^^^^^^^^^^^^^^^^
  |   File "/usr/lib/python3.12/asyncio/runners.py", line 118, in run
  |     return self._loop.run_until_complete(task)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/usr/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
  |     return future.result()
  |            ^^^^^^^^^^^^^^^
  |   File "/home/graingert/.virtualenvs/anyio_fixture_methods/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 2335, in wrapper
  |     return await func(*args)
  |            ^^^^^^^^^^^^^^^^^
  |   File "/home/graingert/projects/demo.py", line 8, in main
  |     async with anyio.create_task_group() as tg:
  |   File "/home/graingert/.virtualenvs/anyio_fixture_methods/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 815, in __aexit__
  |     raise BaseExceptionGroup(
  | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Exception Group Traceback (most recent call last):
    |   File "/home/graingert/projects/demo.py", line 9, in main
    |     raise MyExceptionGroup("hello", [ValueError("bad")])
    | MyExceptionGroup: hello (1 sub-exception)
    +-+---------------- 1 ----------------
      | ValueError: bad
      +------------------------------------

I get a different output on trio:

  + Exception Group Traceback (most recent call last):
  |   File "/home/graingert/projects/demo.py", line 11, in <module>
  |     anyio.run(main, backend="trio")
  |   File "/home/graingert/.virtualenvs/anyio_fixture_methods/lib/python3.12/site-packages/anyio/_core/_eventloop.py", line 74, in run
  |     return async_backend.run(func, args, {}, backend_options)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/home/graingert/.virtualenvs/anyio_fixture_methods/lib/python3.12/site-packages/anyio/_backends/_trio.py", line 1000, in run
  |     return trio.run(func, *args)
  |            ^^^^^^^^^^^^^^^^^^^^^
  |   File "/home/graingert/.virtualenvs/anyio_fixture_methods/lib/python3.12/site-packages/trio/_core/_run.py", line 2306, in run
  |     raise runner.main_task_outcome.error
  |   File "/home/graingert/projects/demo.py", line 8, in main
  |     async with anyio.create_task_group() as tg:
  |   File "/home/graingert/.virtualenvs/anyio_fixture_methods/lib/python3.12/site-packages/anyio/_backends/_trio.py", line 191, in __aexit__
  |     return await self._nursery_manager.__aexit__(exc_type, exc_val, exc_tb)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/home/graingert/.virtualenvs/anyio_fixture_methods/lib/python3.12/site-packages/trio/_core/_run.py", line 959, in __aexit__
  |     raise combined_error_from_nursery
  | ExceptionGroup: Exceptions from Trio nursery (1 sub-exception)
  +-+---------------- 1 ----------------
    | Exception Group Traceback (most recent call last):
    |   File "/home/graingert/projects/demo.py", line 9, in main
    |     raise MyExceptionGroup("hello", [ValueError("bad")])
    | ExceptionGroup: hello (1 sub-exception)
    +-+---------------- 1 ----------------
      | ValueError: bad
      +------------------------------------
@graingert graingert added the bug Something isn't working label Dec 29, 2024
@graingert
Copy link
Collaborator Author

graingert commented Dec 29, 2024

looks like this is a bug in trio python-trio/trio#3175, sorry:

import trio

class MyExceptionGroup(ExceptionGroup):
    pass


async def main():
    async with trio.open_nursery():
        raise MyExceptionGroup("hello", [ValueError("bad")])

trio.run(main)
 + Exception Group Traceback (most recent call last):
  |   File "/home/graingert/projects/demo.py", line 11, in <module>
  |     trio.run(main)
  |   File "/home/graingert/.virtualenvs/anyio_fixture_methods/lib/python3.12/site-packages/trio/_core/_run.py", line 2306, in run
  |     raise runner.main_task_outcome.error
  |   File "/home/graingert/projects/demo.py", line 8, in main
  |     async with trio.open_nursery():
  |   File "/home/graingert/.virtualenvs/anyio_fixture_methods/lib/python3.12/site-packages/trio/_core/_run.py", line 959, in __aexit__
  |     raise combined_error_from_nursery
  | ExceptionGroup: Exceptions from Trio nursery (1 sub-exception)
  +-+---------------- 1 ----------------
    | Exception Group Traceback (most recent call last):
    |   File "/home/graingert/projects/demo.py", line 9, in main
    |     raise MyExceptionGroup("hello", [ValueError("bad")])
    | ExceptionGroup: hello (1 sub-exception)
    +-+---------------- 1 ----------------
      | ValueError: bad
      +------------------------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant