Skip to content

Commit

Permalink
fix: Avoid crashes when scope or hub is racy (#517)
Browse files Browse the repository at this point in the history
* fix: Avoid crashes when scope or hub is racy

* fix: Fix None deref
  • Loading branch information
untitaker committed Oct 1, 2019
1 parent 0fb630e commit fae46f6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
13 changes: 8 additions & 5 deletions sentry_sdk/hub.py
Expand Up @@ -440,8 +440,9 @@ def start_span(
kwargs.setdefault("hub", self)

if span is None:
if scope.span is not None:
span = scope.span.new_span(**kwargs)
span = scope.span
if span is not None:
span = span.new_span(**kwargs)
else:
span = Span(**kwargs)

Expand Down Expand Up @@ -570,17 +571,19 @@ def iter_trace_propagation_headers(self):
# type: () -> Generator[Tuple[str, str], None, None]
# TODO: Document
client, scope = self._stack[-1]
if scope._span is None:
span = scope.span

if span is None:
return

propagate_traces = client and client.options["propagate_traces"]
if not propagate_traces:
return

if client and client.options["traceparent_v2"]:
traceparent = scope._span.to_traceparent()
traceparent = span.to_traceparent()
else:
traceparent = scope._span.to_legacy_traceparent()
traceparent = span.to_legacy_traceparent()

yield "sentry-trace", traceparent

Expand Down
17 changes: 13 additions & 4 deletions sentry_sdk/scope.py
Expand Up @@ -66,6 +66,12 @@ class Scope(object):
events that belong to it.
"""

# NOTE: Even though it should not happen, the scope needs to not crash when
# accessed by multiple threads. It's fine if it's full of races, but those
# races should never make the user application crash.
#
# The same needs to hold for any accesses of the scope the SDK makes.

__slots__ = (
"_level",
"_name",
Expand Down Expand Up @@ -124,8 +130,9 @@ def transaction(self, value):
# type: (Optional[str]) -> None
"""When set this forces a specific transaction name to be set."""
self._transaction = value
if self._span:
self._span.transaction = value
span = self._span
if span:
span.transaction = value

@_attr_setter
def user(self, value):
Expand All @@ -143,8 +150,10 @@ def span(self):
def span(self, span):
# type: (Optional[Span]) -> None
self._span = span
if span is not None and span.transaction:
self._transaction = span.transaction
if span is not None:
span_transaction = span.transaction
if span_transaction:
self._transaction = span_transaction

def set_tag(
self,
Expand Down

0 comments on commit fae46f6

Please sign in to comment.