Bug 29906

Summary: Static method TimeZoneInfo.GetSystemTimeZones() is not thread safe.
Product: [Mono] Class Libraries Reporter: af4178a2 <xamarin>
Component: SystemAssignee: Marek Safar <masafa>
Severity: normal CC: masafa, mono-bugs+mono, xamarin
Priority: ---    
Version: 3.2.x   
Target Milestone: Untriaged   
Hardware: PC   
OS: Linux   
Tags: Is this bug a regression?: ---
Last known good build:

Description af4178a2 2015-05-09 16:27:07 UTC
If multiple threads are concurrently calling TimeZoneInfo.GetSystemTimeZones() the faster thread initializes the static field "systemTimeZones" and populates the list. A slower thread either immediatelly retrieves an indirect reference to the same list which is still populated by the first thread or instantiates the field and populates the list itself again.

Consider to lock the block which is responsible for initializing the static field "systemTimeZones" until it is completely populated.

The problem still exists in the latest mono/master branch. A short look to the code and you can see the problem immediately.

642			private static List<TimeZoneInfo> systemTimeZones;
643			public static ReadOnlyCollection<TimeZoneInfo> GetSystemTimeZones ()
644			{
645				if (systemTimeZones == null) {
646				systemTimeZones = new List<TimeZoneInfo> ();
...				// list is populated
694				}
695				return new ReadOnlyCollection<TimeZoneInfo> (systemTimeZones);
696			}
Comment 1 Marek Safar 2015-05-25 08:57:24 UTC
Fixed in master