-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Activity.Current does not reset to null if entry span stops/disposes before child spans causing all subsequent activities to share the same trace id #91265
Comments
Tagging subscribers to this area: @tarekgh, @tommcdon, @pjanotti Issue DetailsDescriptionStopping an entry span (one that starts a trace) before its children 'leaks' as This can erroneously cause all subsequent activities to reuse the 'leaked' Reproduction StepsUsing the following minimal bootstrap: using System.Diagnostics;
var listener = new ActivityListener
{
ShouldListenTo = _ => true,
Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData
};
ActivitySource.AddActivityListener(listener); Starting 3 traces but disposing the entry span before its children: // Activity.Current == null here (correctly)
for (var i = 0; i < 3; i++)
{
Console.WriteLine($"=> Trace {i}");
using var entry = source.StartActivity()!;
Console.WriteLine($"=> Active after entry start: {Activity.Current?.Id}");
//using var child = source.StartActivity();
using var span = new Activity("child").Start();
entry.Stop();
Console.WriteLine($"=> Active after entry stop : {Activity.Current?.Id}");
child.Stop();
Console.WriteLine($"=> Active after child stop : {Activity.Current?.Id}");
Console.WriteLine();
}
if (Activity.Current != null)
Console.WriteLine("Still an active trace after all activities are disposed"); Will yield:
We can clearly see the On any subsequent iteration the will reuse the Expected behaviorfor (var i = 0; i < 3; i++)
{
using var entry = source.StartActivity()!;
Console.WriteLine($"=> Active after entry start: {Activity.Current?.Id}");
using var child = new Activity("child").Start();
}
if (Activity.Current == null)
Console.WriteLine("Correctly reset `Activity.Current` to null as there are no active traces anymore"); or even more explicitly for (var i = 0; i < 3; i++)
{
using var entry = source.StartActivity()!;
Console.WriteLine($"=> Active after entry start: {Activity.Current?.Id}");
using var child = new Activity("child").Start();
child.Stop();
entry.Stop();
}
if (Activity.Current == null)
Console.WriteLine("Correctly reset `Activity.Current` to null as there are no active traces anymore"); Will yield something similar to:
Ensuring each iteration correctly starts a new trace and after the for loop The only way to clear this state is to manually set Actual behaviorStopping activities out of order may cause Regression?No response Known WorkaroundsNo response Configuration.NET <= 7.0 Other informationNo response
|
@Mpdreamz did you run into this issue in a real scenario? |
Tagging subscribers to this area: @dotnet/area-system-diagnostics-activity Issue DetailsDescriptionStopping an entry span (one that starts a trace) before its children 'leaks' as This can erroneously cause all subsequent activities to reuse the 'leaked' Reproduction StepsUsing the following minimal bootstrap: using System.Diagnostics;
var listener = new ActivityListener
{
ShouldListenTo = _ => true,
Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData
};
ActivitySource.AddActivityListener(listener); Starting 3 traces but disposing the entry span before its children: // Activity.Current == null here (correctly)
for (var i = 0; i < 3; i++)
{
Console.WriteLine($"=> Trace {i}");
using var entry = source.StartActivity()!;
Console.WriteLine($"=> Active after entry start: {Activity.Current?.Id}");
//using var child = source.StartActivity();
using var span = new Activity("child").Start();
entry.Stop();
Console.WriteLine($"=> Active after entry stop : {Activity.Current?.Id}");
child.Stop();
Console.WriteLine($"=> Active after child stop : {Activity.Current?.Id}");
Console.WriteLine();
}
if (Activity.Current != null)
Console.WriteLine("Still an active trace after all activities are disposed"); Will yield:
We can clearly see the On any subsequent iteration the will reuse the Expected behaviorfor (var i = 0; i < 3; i++)
{
using var entry = source.StartActivity()!;
Console.WriteLine($"=> Active after entry start: {Activity.Current?.Id}");
using var child = new Activity("child").Start();
}
if (Activity.Current == null)
Console.WriteLine("Correctly reset `Activity.Current` to null as there are no active traces anymore"); or even more explicitly for (var i = 0; i < 3; i++)
{
using var entry = source.StartActivity()!;
Console.WriteLine($"=> Active after entry start: {Activity.Current?.Id}");
using var child = new Activity("child").Start();
child.Stop();
entry.Stop();
}
if (Activity.Current == null)
Console.WriteLine("Correctly reset `Activity.Current` to null as there are no active traces anymore"); Will yield something similar to:
Ensuring each iteration correctly starts a new trace and after the for loop The only way to clear this state is to manually set Actual behaviorStopping activities out of order may cause Regression?No response Known WorkaroundsNo response Configuration.NET <= 7.0 Other informationNo response
|
This issue has been marked |
This issue has been automatically marked |
@tarekgh if meaning in one the existing opentelemetry integrations, no. However I do believe this to be a general API problem. It would be quite common for someone to want to start a trace over various asynchronous work without being able to ensure the entry/starting span is not disposed before all this asynchronous work is completed. |
Description
Stopping an entry span (one that starts a trace) before its children 'leaks' as
Activity.Current
never gets cleared out tonull
again.This can erroneously cause all subsequent activities to reuse the 'leaked'
trace id
.Reproduction Steps
Using the following minimal bootstrap:
Starting 3 traces but disposing the entry span before its children:
Will yield:
We can clearly see the
child.Stop()
on the first trace will reinstateentry
as theActivity.Current
even thoughentry
was already stopped.On any subsequent iteration the will reuse the
traceid
of the first trace and continue it rather than creating a new one.Expected behavior
or even more explicitly
Will yield something similar to:
Ensuring each iteration correctly starts a new trace and after the for loop
Activity.Current
is correctly reset tonull
.The only way to clear this state is to manually set
Activity.Current
tonull
.Actual behavior
Stopping activities out of order may cause
Activity.Current
to stick to a certaintraceid
.Regression?
No response
Known Workarounds
No response
Configuration
.NET <= 7.0
Other information
No response
The text was updated successfully, but these errors were encountered: