Fix InvalidOperationException in AvTrace and improve general thread-safety#9352
Fix InvalidOperationException in AvTrace and improve general thread-safety#9352h3xds1nz wants to merge 2 commits into
Conversation
|
While digging into this; there's one race-condition I've found along the way after fixing the linked issue, which ain't a WPF issue but imperfection/bug in the thread-safety of When calling The easiest workaround in that case is to just call first More simply speaking, using the repro project in #9347 with my PR (or without, with multiple threads but harder to catch as it usually dies on |
|
The dotnet/runtime issue has been now fixed for .NET 9, so this now works flawlessly with my PR. |
906f734 to
277e47f
Compare
|
Resolved merge conflicts 05 |
Fixes #9347
Description
As I believe
AvTraceshould be close to thread-safe class and it seems the original intention was for it to be from the comments, especially since even running during debug will have another thread initializing a source, based on the report in issue #9347, I've decided to create a little improvement.IsWpfTracingEnabledInRegistryget what they came for)s_enabledInRegistryRefresh, we reload the setting from registry and update the value ins_enabledInRegistryaccordinglyIsWpfTracingEnabledInRegistrynow merely retrieves the latest value for statics_enabledInRegistrys_enabledInRegistrydoesn't necessarily need to be volatile but I felt like the memory fence here is worth it for consistency.AvTraceinstances/refreshes in general.Initializefunction betweenShouldCreateTraceSourceswhere a different value could have been obtained fromIsWpfTracingEnabledInRegistryafterwards if changed by a different thread meanwhile; if the setting in registry was changed.It is not perfect, but it prevents the exception problem and improves the behaviour overall.
Customer Impact
This PR fixes following behavior: Ocassionally,
InvalidOperationExceptionmight be thrown when setting up WPF trace sources, note that the class might be used by multiple threads by default without forcing it; e.g. when debugging.Regression
No.
Testing
Build, local testing, verifying setting retrieval and stability.
Risk
Low.
Microsoft Reviewers: Open in CodeFlow