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

fix loss of context/cause on exceptions raised inside open_websocket #192

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

jakkdl
Copy link
Member

@jakkdl jakkdl commented Oct 20, 2024

fixes #191

I'm not 100% sure that copy_exc is 100% robust (given that everything seems ever so fickle with copying exceptions), so it may be a good idea to wrap it in a try: except that gives up on copying if anything fails.

@jakkdl jakkdl requested a review from graingert October 23, 2024 10:32
@jakkdl
Copy link
Member Author

jakkdl commented Oct 23, 2024

shoutout to @graingert for the clean no-copy solution that avoids the issues mentioned in python-trio/flake8-async#298 and python/cpython#125782

trio_websocket/_impl.py Outdated Show resolved Hide resolved
@jakkdl jakkdl requested a review from graingert October 25, 2024 10:29
Comment on lines +188 to +193
context = exc.__context__
try:
raise exc
finally:
exc.__context__ = context
del exc, context
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just remembered an issue with this:

python/cpython#99856
python-attrs/attrs#1081

Suggested change
context = exc.__context__
try:
raise exc
finally:
exc.__context__ = context
del exc, context
context = exc.__context__
try:
raise exc
finally:
object.__setattr__(exc, "__context__", context)
del exc, context

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll probably need a test with a frozen dataclass and attrs class Exception

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can fix them... but open_websocket is decorated with @asynccontextmanager so it just kicks the can onto failing in the same way as python/cpython#99856

For attrs compatibility I kind of feel like we can require users with frozen exception to require trio_websocket<=0.11.1 or attrs>24.2.0 (both to be released) - or change their exceptions. It's not a heavy workaround for us, but I still don't really think it's worth it.

"the internal exception(s)" + eg_substr + "."
)
user_error.__context__ = BaseExceptionGroup(eg_str, exceptions)
user_error.__suppress_context__ = False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is broken for frozen user_errors on attrs python-attrs/attrs#1365

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 this pull request may close these issues.

__cause__ and __context__ on user exceptions are lost when running with exception groups
3 participants