Skip to content

Fix StartSpan to clear Activity.Current when previous activity is stopped#6991

Merged
rajkumar-rangaraj merged 7 commits intoopen-telemetry:mainfrom
meijeran:fix/tracer-startspan-stopped-previous-activity
Mar 26, 2026
Merged

Fix StartSpan to clear Activity.Current when previous activity is stopped#6991
rajkumar-rangaraj merged 7 commits intoopen-telemetry:mainfrom
meijeran:fix/tracer-startspan-stopped-previous-activity

Conversation

@meijeran
Copy link
Copy Markdown
Contributor

Fixes #6257
Design discussion issue #

Changes

Tracer.StartSpan() restores Activity.Current to previousActivity after creating the new span. If another thread stops previousActivity during span creation, the restore silently fails and leaves the new span as Activity.Current.
The fix sets Activity.Current to null when previousActivity.IsStopped.

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
  • Unit tests added/updated
  • Appropriate CHANGELOG.md files updated for non-trivial changes
  • Changes in public API reviewed (if applicable)

@meijeran meijeran requested a review from a team as a code owner March 24, 2026 18:15
@github-actions github-actions bot added pkg:OpenTelemetry.Api Issues related to OpenTelemetry.Api NuGet package pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package labels Mar 24, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.76%. Comparing base (e10eb61) to head (981c43b).
⚠️ Report is 4 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #6991      +/-   ##
==========================================
+ Coverage   88.74%   88.76%   +0.01%     
==========================================
  Files         263      263              
  Lines       12420    12423       +3     
==========================================
+ Hits        11022    11027       +5     
+ Misses       1398     1396       -2     
Flag Coverage Δ
unittests-Project-Experimental 88.35% <100.00%> (-0.32%) ⬇️
unittests-Project-Stable 88.44% <100.00%> (-0.30%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/OpenTelemetry.Api/Trace/Tracer.cs 93.18% <100.00%> (+0.49%) ⬆️

... and 1 file with indirect coverage changes

&& Activity.Current != previousActivity)
{
Activity.Current = previousActivity;
Activity.Current = previousActivity?.IsStopped == true ? null : previousActivity;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What if previousActivity.Parent is non null and not stopped?
The tree can be deeper.

@meijeran meijeran force-pushed the fix/tracer-startspan-stopped-previous-activity branch from e2de488 to 9dbc981 Compare March 25, 2026 18:59
@meijeran meijeran force-pushed the fix/tracer-startspan-stopped-previous-activity branch from 6934a17 to f2b7f74 Compare March 25, 2026 19:24
@rajkumar-rangaraj rajkumar-rangaraj added this pull request to the merge queue Mar 26, 2026
Merged via the queue into open-telemetry:main with commit a065d9f Mar 26, 2026
63 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

Thank you for your contribution @meijeran! 🎉 We would like to hear from you about your experience contributing to OpenTelemetry by taking a few minutes to fill out this survey.

@meijeran meijeran deleted the fix/tracer-startspan-stopped-previous-activity branch March 26, 2026 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg:OpenTelemetry.Api Issues related to OpenTelemetry.Api NuGet package pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Race condition in Tracer.StartSpan() can throw System.InvalidOperation

4 participants