Skip to content
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

fix: Avoid crashes when scope or hub is racy #517

Merged
merged 2 commits into from Oct 1, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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