Skip to content

Conversation

alexander-alderman-webb
Copy link
Contributor

@alexander-alderman-webb alexander-alderman-webb commented Sep 1, 2025

Closes #374

Adds an uncaught exception integration, enabled by default. The integration forwards the exception to Sentry only if the exception value and stacktrace are set.

The implementation and tests are adapted from excepthook. Sets sys.unraisablehook instead ofsys.excepthook, and changes relevant signatures and types. Compared with the excepthook integration, there is no always_run parameter. As far as I can tell the option was added to avoid excessive errors in interactive shells, which does not apply to unraisable errors.

Unlike in the excepthook tests, the unraisablehook test does not check for the presence of the local variable in the pre-context. The local variable from the excepthook test is not captured in an analogous test with an unraisable exception because an (unraisable) exception in __del__ has a stack frame with only one level.

A good source about unraisable exceptions: https://vstinner.github.io/sys-unraisablehook-python38.html

Adds an uncaught exception integration, enabled by default. The
integration forwards the exception to Sentry only if the exception
value and stacktrace are set.
@alexander-alderman-webb alexander-alderman-webb requested a review from a team as a code owner September 1, 2025 14:18
Copy link

codecov bot commented Sep 1, 2025

Codecov Report

❌ Patch coverage is 85.71429% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.89%. Comparing base (b1a8b63) to head (2f2fef5).
⚠️ Report is 7 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
sentry_sdk/integrations/unraisablehook.py 85.71% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4733      +/-   ##
==========================================
- Coverage   84.92%   84.89%   -0.03%     
==========================================
  Files         156      157       +1     
  Lines       16115    16136      +21     
  Branches     2741     2744       +3     
==========================================
+ Hits        13685    13698      +13     
- Misses       1643     1651       +8     
  Partials      787      787              
Files with missing lines Coverage Δ
sentry_sdk/integrations/unraisablehook.py 85.71% <85.71%> (ø)

... and 5 files with indirect coverage changes

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@alexander-alderman-webb alexander-alderman-webb marked this pull request as draft September 1, 2025 14:49
@alexander-alderman-webb alexander-alderman-webb marked this pull request as ready for review September 2, 2025 12:38
Copy link
Contributor

@sentrivana sentrivana left a comment

Choose a reason for hiding this comment

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

The integration looks good 💪🏻 Left two comments, please have a look

Comment on lines 77 to 81
if sys.version_info >= (3, 8):
_DEFAULT_INTEGRATIONS.append(
"sentry_sdk.integrations.unraisablehook.UnraisablehookIntegration"
)

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd not make this default just yet, let's give it some time to soak. We can turn it on by default in 3.0 on the potel-base branch -- could you prepare a PR for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes sounds good! #4749

Comment on lines +8 to +11
TEST_PARAMETERS = [
("", "HttpTransport"),
('_experiments={"transport_http2": True}', "Http2Transport"),
]
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to test this both with and without HTTP/2? Is the behavior of the integration any different?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, this is copied over from test_excepthook. I guess the test is more of an integration test as it checks that the unraisable exception goes all the way to the transport.

I'm happy to change this. Would you just use Http2Transport now that will be the default?

Copy link
Contributor

Choose a reason for hiding this comment

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

Didn't realize we're doing the same in the excepthook tests. I guess fine to keep as is then.

Copy link
Contributor

@sentrivana sentrivana left a comment

Choose a reason for hiding this comment

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

🚀

@alexander-alderman-webb alexander-alderman-webb merged commit a6e3b50 into master Sep 3, 2025
135 checks passed
@alexander-alderman-webb alexander-alderman-webb deleted the webb/unraisable-integration branch September 3, 2025 14:49
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.

New integration for sys.unraisablehook
2 participants