From f282e260bb67cd1c9424d12a16bd10b95101c882 Mon Sep 17 00:00:00 2001 From: Hashim1999164 Date: Sun, 4 Oct 2026 03:16:03 +0500 Subject: [PATCH] Clear scope transaction name when span is cleared Restoring scope.span to None after a transaction ends left a stale _transaction value on the scope, so later errors could inherit the old transaction name. Clear it when span is set to None. Fixes getsentry/sentry-python#7774 --- sentry_sdk/scope.py | 4 ++++ tests/tracing/test_misc.py | 3 +++ 2 files changed, 7 insertions(+) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 0d3a0c6cd3..96b2bb5594 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -904,6 +904,10 @@ def span(self) -> "Optional[Span]": @span.setter def span(self, span: "Optional[Span]") -> None: self._span = span + if span is None: + self._transaction = None + self._transaction_info = {} + return # XXX: this differs from the implementation in JS, there Scope.setSpan # does not set Scope._transactionName. if isinstance(span, Transaction): diff --git a/tests/tracing/test_misc.py b/tests/tracing/test_misc.py index 9690f2ffc0..dd6bbc9f33 100644 --- a/tests/tracing/test_misc.py +++ b/tests/tracing/test_misc.py @@ -428,6 +428,9 @@ def test_start_transaction_updates_scope_name_source(sentry_init): assert scope._transaction == "foobar" assert scope._transaction_info == {"source": "route"} + assert scope._transaction is None + assert scope._transaction_info == {} + @pytest.mark.parametrize("sampled", (True, None)) def test_transaction_dropped_debug_not_started(sentry_init, sampled):