Skip to content

fix: clear Scope._transaction when the span is unset - #7851

Closed
intertermux-code wants to merge 2 commits into
getsentry:masterfrom
intertermux-code:fix/7774-scope-transaction-clear
Closed

intertermux-code wants to merge 2 commits into
getsentry:masterfrom
intertermux-code:fix/7774-scope-transaction-clear

Conversation

@intertermux-code

Copy link
Copy Markdown
Contributor

Description

Scope.span's setter copies a Transaction's name into scope._transaction, but nothing ever cleared it: when a top-level transaction finishes, Span.__exit__ restores scope.span to the previous span (None), and the stale name stayed on the scope. Any event captured later on that scope then inherited the finished transaction's name (e.g. an unrelated error showing up under the wrong transaction name).

The fix adds an elif span is None branch to the setter that resets _transaction. It deliberately does not clear on plain child spans: a finishing child span restores the transaction itself (not None), so a live transaction's name is preserved — covered by the nested test below.

Added:

  • test_transaction_name_cleared_when_span_unset: Transaction -> child span -> None
  • test_transaction_name_survives_nested_span_exit: nested spans keep the live transaction's name, which is cleared once the transaction itself exits

Issues

Reminders

  • Please add tests to validate your changes, and lint your code using uv run ruff.
  • Add GH Issue ID & Linear ID (if applicable)
  • PR title should use conventional commit style (feat:, fix:, meta:)

The span setter copied a Transaction's name into _transaction but never
cleared it, so after a top-level transaction finished (span restored to
None) later events on the same scope kept the stale transaction name.
The clear only happens on None: a finishing child span restores the
transaction itself, so a live transaction's name is preserved.

Fixes getsentry#7774.
@intertermux-code
intertermux-code requested a review from a team as a code owner October 5, 2026 05:49

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit fe91a89. Configure here.

Comment thread sentry_sdk/scope.py
Follow-up to the transaction-clearing change: unsetting the span reset
_transaction but left _transaction_info behind, so events captured later
on the same scope still received the finished transaction's source via
_apply_transaction_info_to_event. Reset it alongside, matching Scope.clear().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scope._transaction not cleared when span is set to None

2 participants