[release/6.0] Avoid deadlock with ConfigurationManager #63816
Merged
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.

Backport of #62209 to release/6.0
Customer Impact
The following is from a comment @martincostello who originally reported the issue.
I'm not sure if any others have run into the issue, but @martincostello's app wasn't doing anything that unusual. Calling
IConfigurationRoot.Reload()isn't super common but it is something I've seen in other apps. Adding or removing config sources at runtime is a new capability ofConfigurationManagerand doing that could have also had similar consequences.Testing
Regression tests were added.
Risk
Medium.
I'm personally confident in the correctness of the change, but I wish it could have been a more surgical fix. The fix removes a lock that was being taken to avoid disposing
IConfigurationProviders while they were in use and instead adds reference counting logic to serve the same purpose without needing to lock while calling into arbitraryIConfigurationProviderimplementations.ConfigurationManageris new in .NET 6 which mitigates the risk somewhat. However, it is used by ASP.NET Core's newWebApplicationwhich is now the default host in all the .NET 6 ASP.NET Core project templates, but I think that's all the more reason to fix this deadlock in .NET 6.