You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi Team:
I found that Activity.Current is returning a outdated Activity when start new activity in an async scenario, because Activity.Current's type is AsyncLocal and Activity's Parent is passed to mutilple thread but Activity only stopped on single thread.;
I started the ActivityParent in ThreadParent, then call and await an async task on ThreadChild in ActivityA's scope, create the ActivityChild on ThreadChild, ActivityChild is having ActivityParent as its parent, which is good. Then I stopped ActivityChild on ThreadChild, ThreadChild's Activity.Current is rolled back to ActivityChild's parent (ActicityParent).
Then program go back to ThreadParent and ActivityParent stops on ThreadParent, ThreadParent's Activity.Current rolls back to ActivityParent's parent (null), which is good.
ThreadChild is designed to be alive and waiting for other jobs, but ActivityParent will never stop again on ThreadChild, so that ThreadChild's Activity.Current keeps returnning outdated ActivityParent as new Activity's parent on ThreadChild, and can not be rolled back to null any more.
Reproduction Steps
staticasync Task Main(string[]args){varlistener=new ActivityListener
{ShouldListenTo=(x)=>true,Sample=(refActivityCreationOptions<ActivityContext>options)=> ActivitySamplingResult.AllDataAndRecorded,ActivityStarted=(x)=>{ Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} [TID={Environment.CurrentManagedThreadId,3}] ActivityStarted: {x.DisplayName} (Parent={x.Parent?.DisplayName ??"[null]"}) (Current={Activity.Current?.DisplayName ??"[null]"})");},ActivityStopped=(x)=>{ Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} [TID={Environment.CurrentManagedThreadId,3}] ActivityStopped: {x.DisplayName} (Parent={x.Parent?.DisplayName ??"[null]"}) (Current={Activity.Current?.DisplayName ??"[null]"})");}};
ActivitySource.AddActivityListener(listener);
Activity.CurrentChanged +=(s,e)=>{ Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} [TID={Environment.CurrentManagedThreadId,3}] \t\tCurrentChanged: {e.Previous?.DisplayName ??"[null]"} => {e.Current?.DisplayName ??"[null]"}");};varsource=new ActivitySource("TestActivitySource");
Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} [TID={Environment.CurrentManagedThreadId,3}] Current activity before Main: {Activity.Current?.DisplayName ??"[null]"}");using(varactivity= source.StartActivity("ActivityParent")!){
Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} [TID={Environment.CurrentManagedThreadId,3}] Current activity in Main: {Activity.Current?.DisplayName ??"[null]"}");vartask= AsyncTask();
Thread.Sleep(30);
Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} [TID={Environment.CurrentManagedThreadId,3}] Current activity in Main after async task: {Activity.Current?.DisplayName ??"[null]"}");await task;}
Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} [TID={Environment.CurrentManagedThreadId,3}] Current activity after Main: {Activity.Current?.DisplayName ??"[null]"}");
Console.Read();}staticasync Task AsyncTask(){varsource=new ActivitySource("TestActivitySource");
Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} [TID={Environment.CurrentManagedThreadId,3}] Current activity before async task: {Activity.Current?.DisplayName ??"[null]"}");using(varactivity= source.StartActivity("ActivityChild")!){
Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} [TID={Environment.CurrentManagedThreadId,3}] Current activity in async task: {Activity.Current?.DisplayName ??"[null]"}");await Task.Delay(10);}// Here, ThreadChild's Activity.Current is ActivityParent instead of null, which is bad.
Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} [TID={Environment.CurrentManagedThreadId,3}] Current activity after async task: {Activity.Current?.DisplayName ??"[null]"}");}
Expected behavior
Return an Activity object with correct parent from ActivitySource.StartActivity() in async scenario.
Actual behavior
Activity.Current can not roll back correctly once activitywas passed over multiple threads.
Regression?
No response
Known Workarounds
No response
Configuration
.Net 8.0
Windows Server 2022
x64
Other information
No response
The text was updated successfully, but these errors were encountered:
Description
Hi Team:
I found that Activity.Current is returning a outdated Activity when start new activity in an async scenario, because Activity.Current's type is AsyncLocal and Activity's Parent is passed to mutilple thread but Activity only stopped on single thread.;
I started the ActivityParent in ThreadParent, then call and await an async task on ThreadChild in ActivityA's scope, create the ActivityChild on ThreadChild, ActivityChild is having ActivityParent as its parent, which is good.
Then I stopped ActivityChild on ThreadChild, ThreadChild's Activity.Current is rolled back to ActivityChild's parent (ActicityParent).
Then program go back to ThreadParent and ActivityParent stops on ThreadParent, ThreadParent's Activity.Current rolls back to ActivityParent's parent (null), which is good.
ThreadChild is designed to be alive and waiting for other jobs, but ActivityParent will never stop again on ThreadChild, so that ThreadChild's Activity.Current keeps returnning outdated ActivityParent as new Activity's parent on ThreadChild, and can not be rolled back to null any more.
Reproduction Steps
Expected behavior
Return an Activity object with correct parent from ActivitySource.StartActivity() in async scenario.
Actual behavior
Activity.Current can not roll back correctly once activitywas passed over multiple threads.
Regression?
No response
Known Workarounds
No response
Configuration
.Net 8.0
Windows Server 2022
x64
Other information
No response
The text was updated successfully, but these errors were encountered: