EventSource.IsEnabled() may start returning true before the OnEventCommand completes.
We initialize shared EventCounter instances in OnEventCommand that we then use from instrumented code paths.
If the EventSource is initialized before being enabled, and the instance is accessed from multiple threads, a thread could see IsEnabled() == true and then read a nullEventCounter instance.
This race condition only appears once per process.
I believe this is the cause of an exception YARP hit in CI:
System.NullReferenceException : Object reference not set to an instance of an object.
at System.Net.Http.HttpTelemetry.Http11RequestLeftQueue(Double timeOnQueueMilliseconds)
at System.Net.Http.HttpConnectionPool.GetHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
We should fix these cases.
cc: @Tratcher aspnetcore has 2 such cases as well:
https://github.com/dotnet/aspnetcore/blob/1e8fe7e4ba93935ebc10aee6ef2a4d2b3b0c05d9/src/Middleware/ConcurrencyLimiter/src/ConcurrencyLimiterEventSource.cs#L47
https://github.com/dotnet/aspnetcore/blob/1e8fe7e4ba93935ebc10aee6ef2a4d2b3b0c05d9/src/SignalR/common/Http.Connections/src/Internal/HttpConnectionsEventSource.cs#L46
EventSource.IsEnabled()may start returningtruebefore theOnEventCommandcompletes.We initialize shared
EventCounterinstances inOnEventCommandthat we then use from instrumented code paths.If the
EventSourceis initialized before being enabled, and the instance is accessed from multiple threads, a thread could seeIsEnabled() == trueand then read anullEventCounterinstance.This race condition only appears once per process.
I believe this is the cause of an exception YARP hit in CI:
We should fix these cases.
cc: @Tratcher aspnetcore has 2 such cases as well:
https://github.com/dotnet/aspnetcore/blob/1e8fe7e4ba93935ebc10aee6ef2a4d2b3b0c05d9/src/Middleware/ConcurrencyLimiter/src/ConcurrencyLimiterEventSource.cs#L47
https://github.com/dotnet/aspnetcore/blob/1e8fe7e4ba93935ebc10aee6ef2a4d2b3b0c05d9/src/SignalR/common/Http.Connections/src/Internal/HttpConnectionsEventSource.cs#L46