Skip to content

fix: unbind finished span from scope regardless of sampling - #2158

Merged
tustanivsky merged 2 commits into
masterfrom
fix/span-finish-scope-cleanup
Oct 2, 2026
Merged

tustanivsky merged 2 commits into
masterfrom
fix/span-finish-scope-cleanup

Conversation

@tustanivsky

@tustanivsky tustanivsky commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

sentry_span_finish only unbinds the span from the scope when the span is recorded, i.e. sampled. A bound child of an unsampled transaction therefore stays on the scope after it finishes, and later events report the already-finished span as their trace context. Transaction finish and sentry_span_discard unbind before any early exit, whether the transaction was sampled or not.

Moving the unbind before the early exits would also move its scope flush onto the hot path. SENTRY_WITH_SCOPE_MUT always flushes the scope to the backend, so every span finish would pay for it, including unsampled ones. Instead, the scope is now flushed only if the span was actually bound. That also removes the redundant flush every sampled span finish paid before, even when the scope didn't change.

Key Changes

  • sentry_span_finish_ts unbinds the span right after the null check, before any early exit, matching transaction finish.
  • The scope is flushed only if the span was actually bound.
  • New regression test: unsampled_span_finish_unbinds_from_scope.

sentry_span_finish only unbound the span from the scope when the span was
recorded, so a bound child of an unsampled transaction stayed on the scope
and later events reported the finished span as their trace context.
Unbind right after the null check, like transaction finish does.

The scope is flushed only if the span was actually bound, so the moved
unbind doesn't add a scope flush to every span finish.
@codecov

codecov Bot commented Oct 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.34%. Comparing base (3d564a0) to head (09df8be).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2158      +/-   ##
==========================================
- Coverage   75.40%   75.34%   -0.07%     
==========================================
  Files         103      103              
  Lines       28105    28105              
  Branches     5133     5132       -1     
==========================================
- Hits        21193    21175      -18     
- Misses       5577     5597      +20     
+ Partials     1335     1333       -2     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@limbonaut limbonaut left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good!

Comment thread src/sentry_core.c

sentry_scope_t *scope = sentry__scope_getref();
bool removed = sentry__scope_remove_span_value(scope, opaque_span->inner);
sentry__scope_finish_mut(scope, removed);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good call!

@jpnurmi jpnurmi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

@tustanivsky
tustanivsky merged commit 636593d into master Oct 2, 2026
66 checks passed
@tustanivsky
tustanivsky deleted the fix/span-finish-scope-cleanup branch October 2, 2026 15:12
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.

3 participants