Skip to content

SystemExit not capturable #2636

@MaximeLaurenty

Description

@MaximeLaurenty

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.39.2

Steps to Reproduce

SystemExit (raised manually or through sys.exit) are not captured by sentry_sdk.
We are not using any wsgi framework: sentry is used to monitor script (running directly through python myscript.py)

I found no way to specify that I didn't want them to be ignored after looking at the doc and the codebase. I only found this issue which had them handled in WSGI integration.

I understand some people might not want these to be captured, but not having the option to capture them seems like a bug to me.

We can manually trigger sentry when sys.exit is called in our code, but it'd be quite painful having to patch all potential packages raising sys.exit (like argparse).

minimal working example:
Python 3.9.16

import atexit
import sys

import sentry_sdk


def before_send(event, hint):
    print("in before_send")
    return event


sentry_sdk.init(
    dsn="obfuscated",
    attach_stacktrace=True,  # attach stacktrace to logs in addition to errors
    environment="local",
    before_send=before_send,
    # doc: https://docs.sentry.io/platforms/python/configuration/options/
)
atexit.register(lambda: sentry_sdk.flush())  # removes error logs on shutdown


def myfunction():
    exit = 2 # tried 1 & 2

    print(f"running sys.exit({exit})")
    sys.exit(exit)  # not triggering before_send

    # print(f"raise systemexit {exit}")
    # raise SystemExit(exit) # not triggering before_send

    # raise BaseException("test maxime")  # triggers before_send

    raise Exception("test maxime")  # triggers before_send


myfunction()

Expected Result

All the above are captured by sentry (sys.exit(1), sys.exit(2), raise SystemExit(1), raise SystemExit(2))

Actual Result

None are captured by sentry (sys.exit(1), sys.exit(2), raise SystemExit(1), raise SystemExit(2)) :

(py39) ➜  repo git:(master) ✗ python test.py
running sys.exit(2)
(py39) ➜  repo git:(master) ✗ python test.py
running sys.exit(1)
(py39) ➜  repo git:(master) ✗ python test.py
raise systemexit 1
(py39) ➜  repo git:(master) ✗ python test.py
in before_send
Traceback (most recent call last):
(...)
BaseException

Metadata

Metadata

Labels

New IntegrationIntegrating with a new framework or libraryTriagedHas been looked at recently during old issue triage
No fields configured for issues without a type.

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions