Fix misguided lock in CurrentTimeZone (#8569)
authorJustin Van Patten <jvp@justinvp.com>
Sat, 10 Dec 2016 06:15:56 +0000 (22:15 -0800)
committerDan Moseley <danmose@microsoft.com>
Sat, 10 Dec 2016 06:15:56 +0000 (22:15 -0800)
CurrentTimeZone locks against a static lock object when modifying a
non-static Hashtable. Instead, use the Hashtable instance itself as the
lock object.

src/mscorlib/src/System/CurrentTimeZone.cs

index 9045343..804bbcc 100644 (file)
@@ -21,7 +21,6 @@ namespace System {
     using System;
     using System.Diagnostics.Contracts;
     using System.Text;
-    using System.Threading;
     using System.Collections;
     using System.Globalization;
     using System.Runtime.CompilerServices;
@@ -33,7 +32,7 @@ namespace System {
     {
         // The per-year information is cached in in this instance value. As a result it can
         // be cleaned up by CultureInfo.ClearCachedData, which will clear the instance of this object
-        private Hashtable m_CachedDaylightChanges = new Hashtable();
+        private readonly Hashtable m_CachedDaylightChanges = new Hashtable();
 
         // Standard offset in ticks to the Universal time if
         // no daylight saving is in used.
@@ -146,21 +145,6 @@ namespace System {
             return new DateTime(tick, DateTimeKind.Local, isAmbiguousLocalDst);
         }
 
-        // Private object for locking instead of locking on a public type for SQL reliability work.
-        private static Object s_InternalSyncObject;
-        private static Object InternalSyncObject
-        {
-            get
-            {
-                if (s_InternalSyncObject == null)
-                {
-                    Object o = new Object();
-                    Interlocked.CompareExchange<Object>(ref s_InternalSyncObject, o, null);
-                }
-                return s_InternalSyncObject;
-            }
-        }
-
         public override DaylightTime GetDaylightChanges(int year)
         {
             if (year < 1 || year > 9999)
@@ -200,7 +184,7 @@ namespace System {
                     currentDaylightChanges = new DaylightTime(DateTime.MinValue, DateTime.MinValue, TimeSpan.Zero); 
                 }
 
-                lock (InternalSyncObject)
+                lock (m_CachedDaylightChanges)
                 {
                     if (!m_CachedDaylightChanges.Contains(objYear))
                     {