Skip to content

Commit 6d184c7

Browse files
authored
Fix InvalidOperationException in Baggage handling in Azure Functions (#7810)
## Summary of changes Fixes `InvalidOperationException` in baggage handling ## Reason for change #7620 added support for event hubs to Azure Functions, but [we've subsequently seen errors related to baggage](https://app.datadoghq.com/error-tracking/issue/a0cbfac8-b924-11f0-a50e-da7ad0900002?query=source%3Adotnet%20%40tracer_version%3A3.30.0.0%20service%3Ainstrumentation-telemetry-data%20-%40error.is_crash%3Atrue&index=&tb=org_id%2C%40rc-client.tracer_version%2C%40org_id&from_ts=1761842690105&to_ts=1763052290105&live=true): ``` Error : Error creating or populating scope. System.InvalidOperationException at Datadog.Trace.Baggage.MergeInto(Baggage destination) at Datadog.Trace.ClrProfiler.AutoInstrumentation.Azure.Functions.AzureFunctionsCommon.CreateIsolatedFunctionScope[T](Tracer tracer, T context) ``` We can reproduce this if you pass the same baggage instance to itself in `MergeInto`, so we guard against that here. > _Why_ are we merging baggage into ourself, you may well ask. Good question... I haven't investigated... that's a problem for @pablomartinezbernardo and @lucaspimentel 😄 ## Implementation details Check that you're not merging into yourself ## Test coverage Added a unit test, confirmed it repros, then fixed it
1 parent 2884c97 commit 6d184c7

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

tracer/src/Datadog.Trace/Baggage.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,12 @@ public void MergeInto(Baggage destination)
378378
ThrowHelper.ThrowArgumentNullException(nameof(destination));
379379
}
380380

381+
if (ReferenceEquals(this, destination))
382+
{
383+
// We're trying to merge with ourselves
384+
return;
385+
}
386+
381387
var sourceItems = _items;
382388

383389
if (sourceItems is null || sourceItems.Count == 0)

tracer/test/Datadog.Trace.Tests/BaggageTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,14 @@ public void MergeInto_AddsAndReplacesItems()
168168
baggage1.GetValueOrDefault("key1").Should().Be("new value");
169169
baggage1.GetValueOrDefault("key2").Should().Be("value2");
170170
}
171+
172+
[Fact]
173+
public void MergeInto_SameInstance()
174+
{
175+
var baggage = new Baggage { { "key1", "value1" } };
176+
177+
baggage.MergeInto(baggage);
178+
baggage.Count.Should().Be(1);
179+
baggage.GetValueOrDefault("key1").Should().Be("value1");
180+
}
171181
}

0 commit comments

Comments
 (0)