Skip to content

Commit 4524b65

Browse files
fix(tracing): Stop setting NoOpSpan on scope in the streaming trace lifecycle (#6844)
Do not set transaction-based spans on the scope when the streaming trace lifecycle is enabled. Early returns in `start_span()` and `start_transaction()` return a `NoOpSpan`, whose `__enter__()` and `__exit__()` methods still mutate the scope.
1 parent 2e9f26e commit 4524b65

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

sentry_sdk/tracing.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import warnings
33
from datetime import datetime, timedelta, timezone
44
from enum import Enum
5-
from typing import TYPE_CHECKING
5+
from typing import TYPE_CHECKING, cast
66

77
import sentry_sdk
88
from sentry_sdk.consts import INSTRUMENTER, SPANDATA, SPANSTATUS, SPANTEMPLATE
@@ -386,6 +386,10 @@ def __repr__(self) -> str:
386386
)
387387

388388
def __enter__(self) -> "Span":
389+
if has_span_streaming_enabled(sentry_sdk.get_client().options):
390+
self._context_manager_state = None # early return sentinel
391+
return self
392+
389393
scope = self.scope or sentry_sdk.get_current_scope()
390394
old_span = scope.span
391395
scope.span = self
@@ -395,11 +399,21 @@ def __enter__(self) -> "Span":
395399
def __exit__(
396400
self, ty: "Optional[Any]", value: "Optional[Any]", tb: "Optional[Any]"
397401
) -> None:
402+
if (
403+
hasattr(self, "_context_manager_state")
404+
and self._context_manager_state is None
405+
):
406+
del self._context_manager_state
407+
return
408+
398409
if value is not None and should_be_treated_as_error(ty, value):
399410
self.set_status(SPANSTATUS.INTERNAL_ERROR)
400411

401412
with capture_internal_exceptions():
402-
scope, old_span = self._context_manager_state
413+
scope, old_span = cast(
414+
"Tuple[sentry_sdk.Scope, Optional[Span]]",
415+
self._context_manager_state,
416+
)
403417
del self._context_manager_state
404418
self.finish(scope)
405419
scope.span = old_span
@@ -1479,6 +1493,7 @@ def calculate_interest_rate(amount, rate, years):
14791493
EnvironHeaders,
14801494
_generate_sample_rand,
14811495
extract_sentrytrace_data,
1496+
has_span_streaming_enabled,
14821497
has_tracing_enabled,
14831498
maybe_create_breadcrumbs_from_span,
14841499
)

0 commit comments

Comments
 (0)