-
Notifications
You must be signed in to change notification settings - Fork 599
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
Profiler cannot reliably be stopped in async methods #623
Comments
It's true that this is a limitation of We could check [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Timing StepIfActive(this MiniProfiler profiler, string name) => profiler?.IsActive == true ? profiler.StepImpl(name) : default; |
I'm using
DefaultProfilerProvider
, which uses anAsyncLocal
to store the current profiler. When I stop+discard the profiler from an async method for the first time,Stop(true) == true
andMiniProfiler.Current == null
. However on a subsequent call of this async methodMiniProfiler.Current
is no longernull
.Stop(true)
will returnfalse
asStopwatch.IsRunning
returnsfalse
. However due toMiniProfiler.Current
not being null, timings will still be collected. I believe the issue is that the logical call context (as used byAsyncLocal
) has copy-on-write behaviour. So when the profiler is stopped, the profiler instance's stopwatch will be stopped and trigger a copy of the logical call context. So nowMiniProfiler.Current
will only benull
for this async method (the current logical context).Due to the copy-on-write behaviour
StopAsync(true)
will not be able to setMiniProfiler.Current
tonull
, as it'll only affect the copied local call context, not the call context of the continuation (the caller).I currently employ the following workaround (you can observe
MiniProfiler.Current != null
after awaiting this method):The text was updated successfully, but these errors were encountered: