You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Scope._transaction not cleared when span is set to None #7774
Start a top-level transaction via OpenAIIntegration (or any path that sets scope.span to a Transaction).
Nest work that restores scope.span to None when the transaction finishes (normal with / start_transaction exit).
Later, on the same Scope (same thread, or a worker that got the Scope via copy_context()), capture an unrelated error with no active transaction.
Expected Result
After scope.span = None, scope._transaction is cleared so later errors are not attributed to the finished transaction (culprit / transaction name should not stick).
Scope.span’s setter sets _transaction when given a Transaction, but does not clear _transaction when span is None. Stale _transaction remains; unrelated errors inherit the old transaction name (e.g. responses gpt-5.6-luna) as culprit even when the stack has nothing to do with that LLM call.
Clearing on every non-Transaction would be wrong: child spans set scope.span = self while a transaction is still live. The fix clears _transaction only when span is None.
Hey @thegunslingers, thanks for the report. I don't believe we can just remove the transaction from the scope if scope.span is set to None. That could unset the transaction prematurely, for instance if you have something like:
importsentry_sdkwithsentry_sdk.start_transaction(...):
withsentry_sdk.start_span(...):
...
# <--- scope.span is unsetwithsentry_sdk.start_span(...):
...
In the example above, when the first span is removed from the scope (which happens when the start_span context manager finishes), the transaction would be removed as well, which is not correct.
What you can try is switching to span streaming, which has no notion of transactions, and the case you describe should work there OOTB. You can switch via:
sentry_sdk.init(
...,
trace_lifecycle="stream",
)
The full migration guide is here. If you're using tracing APIs like start_span or @trace directly or use stuff like before_send_transaction (which has no effect in streaming mode), please give it a read before you migrate.
We'll be phasing transaction-based tracing out in favor of span streaming. In the upcoming major, only span streaming will be available.
Activity
linear-code commentedon Sep 29, 2026
PY-2908
thegunslingers commentedon Sep 29, 2026
I created a draft pr with a proposed fix:
#7773
sentrivana commentedon Sep 30, 2026
Hey @thegunslingers, thanks for the report. I don't believe we can just remove the transaction from the scope if
scope.spanis set toNone. That could unset the transaction prematurely, for instance if you have something like:In the example above, when the first span is removed from the scope (which happens when the
start_spancontext manager finishes), the transaction would be removed as well, which is not correct.What you can try is switching to span streaming, which has no notion of transactions, and the case you describe should work there OOTB. You can switch via:
The full migration guide is here. If you're using tracing APIs like
start_spanor@tracedirectly or use stuff likebefore_send_transaction(which has no effect in streaming mode), please give it a read before you migrate.We'll be phasing transaction-based tracing out in favor of span streaming. In the upcoming major, only span streaming will be available.
fix: clear Scope._transaction when the span is unset
fix: clear Scope._transaction when the span is unset
Neeraj3008 commentedon Oct 5, 2026
is this still open can i work on this?
sentrivana commentedon Oct 6, 2026
I'll close this since we'll be phasing out transaction-based tracing -- span streaming solves the problem.
fix: only clear _transaction when the unset span is a Transaction