Skip to content

Don't set transaction status to error on sys.exit(0) #4025

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

Merged
merged 4 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
is_valid_sample_rate,
logger,
nanosecond_time,
should_be_treated_as_error,
)

from typing import TYPE_CHECKING
Expand Down Expand Up @@ -374,7 +375,7 @@ def __enter__(self):

def __exit__(self, ty, value, tb):
# type: (Optional[Any], Optional[Any], Optional[Any]) -> None
if value is not None:
if value is not None and should_be_treated_as_error(ty, value):
self.set_status(SPANSTATUS.INTERNAL_ERROR)

scope, old_span = self._context_manager_state
Expand Down
9 changes: 9 additions & 0 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1879,3 +1879,12 @@ def get_current_thread_meta(thread=None):

# we've tried everything, time to give up
return None, None


def should_be_treated_as_error(ty, value):
# type: (Any, Any) -> bool
if ty == SystemExit and hasattr(value, "code") and value.code in (0, None):
# https://docs.python.org/3/library/exceptions.html#SystemExit
return False

return True
58 changes: 56 additions & 2 deletions tests/tracing/test_integration_tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import weakref
import gc
import random
import re
import sys
import weakref

import pytest
import random

import sentry_sdk
from sentry_sdk import (
Expand Down Expand Up @@ -297,3 +299,55 @@ def test_trace_propagation_meta_head_sdk(sentry_init):
assert 'meta name="baggage"' in baggage
baggage_content = re.findall('content="([^"]*)"', baggage)[0]
assert baggage_content == transaction.get_baggage().serialize()


@pytest.mark.parametrize(
"exception_cls,exception_value",
[
(SystemExit, 0),
],
)
def test_non_error_exceptions(
sentry_init, capture_events, exception_cls, exception_value
):
sentry_init(traces_sample_rate=1.0)
events = capture_events()

with start_transaction(name="hi") as transaction:
transaction.set_status(SPANSTATUS.OK)
with pytest.raises(exception_cls):
with start_span(op="foo", name="foodesc"):
raise exception_cls(exception_value)

assert len(events) == 1
event = events[0]

span = event["spans"][0]
assert "status" not in span.get("tags", {})
assert "status" not in event["tags"]
assert event["contexts"]["trace"]["status"] == "ok"


@pytest.mark.parametrize("exception_value", [None, 0, False])
def test_good_sysexit_doesnt_fail_transaction(
sentry_init, capture_events, exception_value
):
sentry_init(traces_sample_rate=1.0)
events = capture_events()

with start_transaction(name="hi") as transaction:
transaction.set_status(SPANSTATUS.OK)
with pytest.raises(SystemExit):
with start_span(op="foo", name="foodesc"):
if exception_value is not False:
sys.exit(exception_value)
else:
sys.exit()

assert len(events) == 1
event = events[0]

span = event["spans"][0]
assert "status" not in span.get("tags", {})
assert "status" not in event["tags"]
assert event["contexts"]["trace"]["status"] == "ok"
Loading