-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Fix InvalidOperationException in AvTrace and improve general thread-safety #9352
base: main
Are you sure you want to change the base?
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
AvTrace
should 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.IsWpfTracingEnabledInRegistry
get what they came for)s_enabledInRegistry
Refresh
, we reload the setting from registry and update the value ins_enabledInRegistry
accordinglyIsWpfTracingEnabledInRegistry
now merely retrieves the latest value for statics_enabledInRegistry
s_enabledInRegistry
doesn't necessarily need to be volatile but I felt like the memory fence here is worth it for consistency.AvTrace
instances/refreshes in general.Initialize
function betweenShouldCreateTraceSources
where a different value could have been obtained fromIsWpfTracingEnabledInRegistry
afterwards 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,
InvalidOperationException
might 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