sentry-python icon indicating copy to clipboard operation
sentry-python copied to clipboard

Scope._transaction not cleared when span is set to None

Open thegunslingers opened this issue 2 months ago • 5 comments

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.68.1

Steps to Reproduce

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).

sentry-sdk 2.68.1 (deps: certifi, urllib3>=1.26.11)

Actual Result

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.

thegunslingers avatar Sep 29 '26 19:09 thegunslingers

PY-2908

linear-code[bot] avatar Sep 29 '26 19:09 linear-code[bot]

I created a draft pr with a proposed fix: https://github.com/getsentry/sentry-python/pull/7773

thegunslingers avatar Sep 29 '26 19:09 thegunslingers

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:

import sentry_sdk

with sentry_sdk.start_transaction(...):
    with sentry_sdk.start_span(...):
        ...
    # <--- scope.span is unset
    with sentry_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.

sentrivana avatar Sep 30 '26 06:09 sentrivana

is this still open can i work on this?

Neeraj3008 avatar Oct 05 '26 17:10 Neeraj3008

I'll close this since we'll be phasing out transaction-based tracing -- span streaming solves the problem.

sentrivana avatar Oct 06 '26 08:10 sentrivana