Skip to content

[API] Fix issue with Baggage.Current leaking#7191

Open
martincostello wants to merge 6 commits into
open-telemetry:mainfrom
martincostello:fix-baggage-async-context-leak
Open

[API] Fix issue with Baggage.Current leaking#7191
martincostello wants to merge 6 commits into
open-telemetry:mainfrom
martincostello:fix-baggage-async-context-leak

Conversation

@martincostello
Copy link
Copy Markdown
Member

@martincostello martincostello commented Apr 27, 2026

Changes

Fix issue with Baggage.Current updates made in a child async flow that could leak back into the parent flow.

I got Copilot to write most of this PR. It seems reasonable to me, but this probably needs quite a critical review.

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)

Fix issue with `Baggage.Current` updates made in a child async flow that could leak back into the parent flow.
@github-actions github-actions Bot added the pkg:OpenTelemetry.Api Issues related to OpenTelemetry.Api NuGet package label Apr 27, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.80%. Comparing base (ed44995) to head (252796b).
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #7191      +/-   ##
==========================================
- Coverage   89.86%   89.80%   -0.07%     
==========================================
  Files         275      275              
  Lines       13852    13856       +4     
==========================================
- Hits        12448    12443       -5     
- Misses       1404     1413       +9     
Flag Coverage Δ
unittests-Project-Experimental 89.61% <100.00%> (-0.16%) ⬇️
unittests-Project-Stable 89.79% <100.00%> (-0.08%) ⬇️

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

Files with missing lines Coverage Δ
src/OpenTelemetry.Api/Baggage.cs 100.00% <100.00%> (ø)

... and 4 files with indirect coverage changes

Comment thread src/OpenTelemetry.Api/CHANGELOG.md Outdated
Add PR number.
@martincostello martincostello marked this pull request as ready for review April 27, 2026 20:09
@martincostello martincostello requested a review from a team as a code owner April 27, 2026 20:09
Copilot AI review requested due to automatic review settings April 27, 2026 20:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an ambient-context bug where Baggage.Current mutations performed in child async/parallel flows could leak back into the parent flow by removing shared mutable state from the runtime context slot.

Changes:

  • Update Baggage to store an immutable BaggageHolder in the runtime context slot and replace the holder on every update (instead of mutating a shared holder instance).
  • Add/adjust unit tests to validate isolation of Baggage.Current across async and parallel flows.
  • Add a changelog entry describing the fix.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
test/OpenTelemetry.Api.Tests/BaggageTests.cs Adds/updates tests for async/parallel flow isolation of Baggage.Current.
src/OpenTelemetry.Api/CHANGELOG.md Documents the baggage leak fix in Unreleased notes.
src/OpenTelemetry.Api/Baggage.cs Implements the leak fix by replacing the context slot value on updates and making the holder immutable.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/OpenTelemetry.Api.Tests/BaggageTests.cs Outdated
Comment thread src/OpenTelemetry.Api/Baggage.cs Outdated
Comment thread src/OpenTelemetry.Api/Baggage.cs Outdated
Comment thread src/OpenTelemetry.Api/Baggage.cs Outdated
Comment thread src/OpenTelemetry.Api/Baggage.cs Outdated
Comment thread src/OpenTelemetry.Api/Baggage.cs Outdated
@martincostello martincostello marked this pull request as draft April 27, 2026 20:19
Address review feedback.
@martincostello martincostello marked this pull request as ready for review April 28, 2026 12:51
@Kielek
Copy link
Copy Markdown
Member

Kielek commented Apr 29, 2026

There were some previously reverted changes to this scope.

See the issue #3257
Previous fix #5208 with the comment requesting revert #5208 (comment)

Revert itself #5227


@martinjt, @danelson, @CodeBlanch, FYI

@martincostello martincostello added the keep-open Prevents issues and pull requests being closed as stale label May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

keep-open Prevents issues and pull requests being closed as stale pkg:OpenTelemetry.Api Issues related to OpenTelemetry.Api NuGet package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants