Skip to content

Commit 86d14b0

Browse files
untitakersentry-bot
and
sentry-bot
authored
fix(serialization): Do not crash if tag is nan (getsentry#835)
Co-authored-by: sentry-bot <markus+ghbot@sentry.io>
1 parent db86d61 commit 86d14b0

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

sentry_sdk/serializer.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
import math
23

34
from datetime import datetime
45

@@ -273,7 +274,12 @@ def _serialize_node_impl(
273274
return _flatten_annotated(result)
274275

275276
if obj is None or isinstance(obj, (bool, number_types)):
276-
return obj if not should_repr_strings else safe_repr(obj)
277+
if should_repr_strings or (
278+
isinstance(obj, float) and (math.isinf(obj) or math.isnan(obj))
279+
):
280+
return safe_repr(obj)
281+
else:
282+
return obj
277283

278284
elif isinstance(obj, datetime):
279285
return (

tests/test_client.py

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
capture_exception,
1616
capture_event,
1717
start_transaction,
18+
set_tag,
1819
)
1920
from sentry_sdk.integrations.executing import ExecutingIntegration
2021
from sentry_sdk.transport import Transport
@@ -463,6 +464,10 @@ def test_nan(sentry_init, capture_events):
463464
events = capture_events()
464465

465466
try:
467+
# should_repr_strings=False
468+
set_tag("mynan", float("nan"))
469+
470+
# should_repr_strings=True
466471
nan = float("nan") # noqa
467472
1 / 0
468473
except Exception:
@@ -472,6 +477,7 @@ def test_nan(sentry_init, capture_events):
472477
frames = event["exception"]["values"][0]["stacktrace"]["frames"]
473478
(frame,) = frames
474479
assert frame["vars"]["nan"] == "nan"
480+
assert event["tags"]["mynan"] == "nan"
475481

476482

477483
def test_cyclic_frame_vars(sentry_init, capture_events):

0 commit comments

Comments
 (0)