Race condition during TraceSource Initialization/Refresh #104457
Labels
area-System.Diagnostics.TraceSource
in-pr
There is an active PR which will close this issue when it is merged
regression-from-last-release
Milestone
Description
TraceSource is documented to be a thread-safe, however when you call
System.Diagnostics.Trace.Refresh()
duringTraceSource
initialization from a different thread, you have a high chance of receiving eitherInvalidOperationException
orNullReferenceException
from theOnInitializing
method during the enumeration ofListeners
.There seem to be a lack of synchronization on the collection, this seem to have been introduced in #73087
Reproduction Steps
You can find a demo repository on Github - here.
Expected behavior
I can call
System.Diagnostics.Trace.Refresh()
safely from a different thread during underlyingTraceSource
initialization without the risk of crashing.Actual behavior
A high chance of receiving either
InvalidOperationException
orNullReferenceException
from theOnInitializing
method during the enumeration ofListeners
as the collection might have been modified or initialized from a different thread.Regression?
Since #73087, previous .NET version (6) didn't suffer from this issue.
Known Workarounds
In case of WPF, wait some time before invoking a
Refresh
.Configuration
.NET 8.0.6 x64 Win10
The text was updated successfully, but these errors were encountered: