Repository navigation
Fix baggage leak between Kafka messages - #9529
Conversation
Also introduces a shared type BaggageScope to make this handling consistent between Kafka and Hangfire message processing.
…RestorerAttribute to handle setting the Baggage.Current AsyncLocal in unit tests
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
A timed-out Kafka poll inside an application child scope can orphan the previous consumer’s cleanup state, causing later messages on that execution context to lose consumer spans and baggage extraction.
🤖 Bits Code Review · Commit 7d3769c
BenchmarksBenchmark execution time: 2026-10-30 18:08:44 Comparing candidate commit 905e438 in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 72 metrics, 0 unstable metrics, 5 known flaky benchmarks, 120 flaky benchmarks without significant changes.
|
Execution-Time Benchmarks Report ⏱️Execution-time results for samples comparing This PR (9529) and master. ✅ No regressions detected |
… they run serially
… no-op when double-disposing
…ge has the same lifetime as the span
…e test collection
…tionsCommonBaggageTests
andrewlock
left a comment
There was a problem hiding this comment.
The extra asynclocal worries me a little, as it points to us needing a better API in general (plus concern about the captured value getting out of sync with our async scope manager 😅). But I don't have a better suggestion right now, so
I guess 😁
Summary of changes
This PR updates our Kafka instrumentation's behavior on message consume in the following ways:
Baggageis created from the incoming message headers but it is no longer combined with the ambientBaggage.Currentfrom the worker thread.Baggageis stored inBaggage.Currentfor the duration of the consume callback and the previousBaggage.Currentis restored afterward.Note: This also updates our Hangfire instrumentation because it requires similar functionality, so this PR introduces a shared helper that can be consumed from both.
Reason for change
We had a reported issue where baggage from one Kafka message's headers was set into the consumer thread's ambient Baggage store and repeated on unrelated messages.
Implementation details
This PR manages the consumer span and baggage in the following ways:
KafkaHelper.CreateConsumerScope, the extracted Baggage from the message headers is no longer merged withBaggage.Current, but instead assigned toBaggage.CurrentBaggageScopeclassConsumerContextclass is created that holds a reference to the active ConsumeScope(span) andBaggageScope, which is stored in anAsyncLocalTest coverage
BaggageRestorerAttributewas introduced to handle unit test classes that modifyBaggage.Current, similar toTracerRestorerAttributeBaggageScopeclass as well as regression tests for baggage handling inKafkaHelper.CreateConsumerScopeandKafkaHelper.CloseConsumerScopeOther details
N/A