Bug 18303 - System.Web.Configuration.WebConfigurationManager not thread safe in GetSection(...)
Summary: System.Web.Configuration.WebConfigurationManager not thread safe in GetSectio...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Web (show other bugs)
Version: master
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-03-11 12:54 UTC by Ian Greenhoe
Modified: 2015-02-20 11:24 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Ian Greenhoe 2014-03-11 12:54:20 UTC
This bug is arguable whether it belongs to System.Web.Configuration.WebConfigurationManager, to System.Configuration.Configuration, or to System.Configuration.ConfigurationSectionCollection.

I'm filing the bug against WebConfigurationManager, as I have been able to find a thread safety guarantee against WebConfigurationManager, but not for the underlying classes. It could be fixed at any layer -- my preference would be in ConfigurationSectionCollection.

Analysis:

WebConfigurationManager.GetSection(...) (static variant) is calling into Configuration.GetSection(string name), which in turn is calling into ConfigurationSectionCollection.this[string name].

The problem is that the ConfigurationSectionCollection.this is doing lazy caching of the sections, so if two threads request the same section at the same time when it does not yet exist in the cache, we end up attempting to add the same key twice.

This is in the current trunk,
mcs/class/System.Configuration/System.Configuration/ConfigurationSectionCollection.cs
lines 60-72.

Here's the exception that we are seeing (repeatably):

[System.ArgumentException]: Key duplication when adding: httpModules
  at System.Collections.Hashtable.PutImpl (System.Object key, System.Object value, Boolean overwrite) [0x00000] in <filename unknown>:0 
  at System.Collections.Hashtable.Add (System.Object key, System.Object value) [0x00000] in <filename unknown>:0 
  at System.Collections.Specialized.NameObjectCollectionBase.BaseAdd (System.String name, System.Object value) [0x00000] in <filename unknown>:0 
  at System.Collections.Specialized.NameObjectCollectionBase.BaseSet (System.String name, System.Object value) [0x00000] in <filename unknown>:0 
  at System.Configuration.ConfigurationSectionCollection.get_Item (System.String name) [0x00000] in <filename unknown>:0 
  at System.Configuration.Configuration.GetSection (System.String path) [0x00000] in <filename unknown>:0 
  at System.Web.Configuration.WebConfigurationManager.GetSection (System.String sectionName, System.String path, System.Web.HttpContext context) [0x00000] in <filename unknown>:0 
  at System.Web.Configuration.WebConfigurationManager.GetSection (System.String sectionName, System.String path) [0x00000] in <filename unknown>:0 
  at System.Web.Configuration.WebConfigurationManager.GetWebApplicationSection (System.String sectionName) [0x00000] in <filename unknown>:0 
  at System.Web.HttpApplication.InitOnce (Boolean full_init) [0x00000] in <filename unknown>:0
Comment 1 Miguel de Icaza [MSFT] 2015-02-20 11:24:07 UTC
Applied the patch from:

https://github.com/mono/mono/pull/1582