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

__cause__ and __context__ on user exceptions are lost when running with exception groups #191

Open
jakkdl opened this issue Oct 9, 2024 · 0 comments · May be fixed by #192
Open

__cause__ and __context__ on user exceptions are lost when running with exception groups #191

jakkdl opened this issue Oct 9, 2024 · 0 comments · May be fixed by #192

Comments

@jakkdl
Copy link
Member

jakkdl commented Oct 9, 2024

This code will fully lose the TypeError as the __context__ from the exception, due to python-trio/flake8-async#298

async def test_user_exception_cause(nursery) -> None:
    async def handler(request):
        await request.accept()
    server = await nursery.start(serve_websocket, handler, HOST, 0, None)
    async with open_websocket(HOST, server.port, RESOURCE, use_ssl=False):
        try:
            raise TypeError
        except TypeError:
            raise ValueError

The offending code for this example is

except _TRIO_EXC_GROUP_TYPE as e:
# user_error, or exception bubbling up from _reader_task
if len(e.exceptions) == 1:
raise e.exceptions[0]

which implicitly sets __context__ to the caught group, and the user exception inside the group is the same exception, so it simultaneously loses its old __context__.

The fix is to sprinkle a bunch of copy.copy() calls. Given that we have a finally it's not an alternative to raise the exception outside of the exception handler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant