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

If Exception.__context__ is an ExceptionGroup, Sentry doesn't capture the ExceptionGroup's children #3913

Open
tmaxwell-anthropic opened this issue Jan 10, 2025 · 2 comments
Labels
Component: SDK Core Dealing with the core of the SDK

Comments

@tmaxwell-anthropic
Copy link

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.29.2

Steps to Reproduce

Here's a toy example that demonstrates the problem:

try:
    try:
        raise ExceptionGroup("group", [ValueError("child1"), ValueError("child2")])
    finally:
        raise TypeError("bar")
except BaseException:
    sentry_sdk.capture_exception()

sentry_sdk.capture_exception() receives the TypeError. TypeError.__context__ points to the ExceptionGroup; and then ExceptionGroup.exceptions points to [ValueError("child1"), ValueError("child2")].

Expected Result

The Sentry event should contain all four exceptions: the TypeError, the ExceptionGroup, and the sub-exceptions ValueError("child1") and ValueError("child2").

Additionally, the event should record that the ExceptionGroup is TypeError.__context__ as opposed to TypeError.__cause__.

Note that the sentry_sdk.utils.exceptions_from_error() function actually does this correctly, but it isn't getting invoked in this case. Here's a screenshot of how it looks if I patch the Sentry SDK to always use sentry_sdk.utils.exceptions_from_error():

Image

Actual Result

When the TypeError is passed into sentry_sdk.utils.exceptions_from_error_tuple(), it checks whether the TypeError is an ExceptionGroup or not: https://github.com/getsentry/sentry-python/blob/2.19.2/sentry_sdk/utils.py#L968-L1009 Because the TypeError is not an ExceptionGroup, it calls walk_exception_chain(), which examines TypeError.__context__ to find the ExceptionGroup; but it doesn't recurse into the sub-exceptions ValueError("child1") and ValueError("child2"). So the Sentry event only contains the TypeError and the ExceptionGroup:

Image

Also, the "Related Exceptions" tree in the above screenshot appears to be nonsensical.

I tested this with SDK version 1.29.2, but the bug appears to still be present in the latest version of the code.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jan 10, 2025
@tmaxwell-anthropic
Copy link
Author

(Minor related issue: I noticed that sentry_sdk.utils.exceptions_from_error() doesn't pass parent_id when recursing here and here. That seems like it might have been a bug?)

@antonpirker antonpirker added the Component: SDK Core Dealing with the core of the SDK label Jan 13, 2025
@antonpirker
Copy link
Member

Hey @tmaxwell-anthropic thanks for this great bug report!

I have put this on our list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: SDK Core Dealing with the core of the SDK
Projects
Status: Waiting for: Product Owner
Development

No branches or pull requests

2 participants