Fix InvalidOperationException in AvTrace and improve general thread-safety #9352
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
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