diff --git a/sentry_sdk/integrations/unraisablehook.py b/sentry_sdk/integrations/unraisablehook.py new file mode 100644 index 0000000000..cfb8212c71 --- /dev/null +++ b/sentry_sdk/integrations/unraisablehook.py @@ -0,0 +1,53 @@ +import sys + +import sentry_sdk +from sentry_sdk.utils import ( + capture_internal_exceptions, + event_from_exception, +) +from sentry_sdk.integrations import Integration + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from typing import Callable + from typing import Any + + +class UnraisablehookIntegration(Integration): + identifier = "unraisablehook" + + @staticmethod + def setup_once(): + # type: () -> None + sys.unraisablehook = _make_unraisable(sys.unraisablehook) + + +def _make_unraisable(old_unraisablehook): + # type: (Callable[[sys.UnraisableHookArgs], Any]) -> Callable[[sys.UnraisableHookArgs], Any] + def sentry_sdk_unraisablehook(unraisable): + # type: (sys.UnraisableHookArgs) -> None + integration = sentry_sdk.get_client().get_integration(UnraisablehookIntegration) + + # Note: If we replace this with ensure_integration_enabled then + # we break the exceptiongroup backport; + # See: https://github.com/getsentry/sentry-python/issues/3097 + if integration is None: + return old_unraisablehook(unraisable) + + if unraisable.exc_value and unraisable.exc_traceback: + with capture_internal_exceptions(): + event, hint = event_from_exception( + ( + unraisable.exc_type, + unraisable.exc_value, + unraisable.exc_traceback, + ), + client_options=sentry_sdk.get_client().options, + mechanism={"type": "unraisablehook", "handled": False}, + ) + sentry_sdk.capture_event(event, hint=hint) + + return old_unraisablehook(unraisable) + + return sentry_sdk_unraisablehook diff --git a/tests/integrations/unraisablehook/test_unraisablehook.py b/tests/integrations/unraisablehook/test_unraisablehook.py new file mode 100644 index 0000000000..2f97886ce8 --- /dev/null +++ b/tests/integrations/unraisablehook/test_unraisablehook.py @@ -0,0 +1,56 @@ +import pytest +import sys +import subprocess + +from textwrap import dedent + + +TEST_PARAMETERS = [ + ("", "HttpTransport"), + ('_experiments={"transport_http2": True}', "Http2Transport"), +] + +minimum_python_38 = pytest.mark.skipif( + sys.version_info < (3, 8), + reason="The unraisable exception hook is only available in Python 3.8 and above.", +) + + +@minimum_python_38 +@pytest.mark.parametrize("options, transport", TEST_PARAMETERS) +def test_unraisablehook(tmpdir, options, transport): + app = tmpdir.join("app.py") + app.write( + dedent( + """ + from sentry_sdk import init, transport + from sentry_sdk.integrations.unraisablehook import UnraisablehookIntegration + + class Undeletable: + def __del__(self): + 1 / 0 + + def capture_envelope(self, envelope): + print("capture_envelope was called") + event = envelope.get_event() + if event is not None: + print(event) + + transport.{transport}.capture_envelope = capture_envelope + + init("http://foobar@localhost/123", integrations=[UnraisablehookIntegration()], {options}) + + undeletable = Undeletable() + del undeletable + """.format( + transport=transport, options=options + ) + ) + ) + + output = subprocess.check_output( + [sys.executable, str(app)], stderr=subprocess.STDOUT + ) + + assert b"ZeroDivisionError" in output + assert b"capture_envelope was called" in output diff --git a/tests/test_basics.py b/tests/test_basics.py index 2eeba78216..45303c9a59 100644 --- a/tests/test_basics.py +++ b/tests/test_basics.py @@ -870,6 +870,7 @@ def foo(event, hint): (["celery"], "sentry.python"), (["dedupe"], "sentry.python"), (["excepthook"], "sentry.python"), + (["unraisablehook"], "sentry.python"), (["executing"], "sentry.python"), (["modules"], "sentry.python"), (["pure_eval"], "sentry.python"),