Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit a4af006

Browse files
authored
Break current culture initialization cycle (#15938)
Fixes #15931
1 parent 128893e commit a4af006

File tree

3 files changed

+18
-59
lines changed

3 files changed

+18
-59
lines changed

src/mscorlib/src/System/Globalization/CultureInfo.Unix.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal static CultureInfo GetUserDefaultCulture()
3434
return cultureInfo;
3535
}
3636

37-
private static CultureInfo GetUserDefaultUILanguage()
37+
private static CultureInfo GetUserDefaultUICulture()
3838
{
3939
return GetUserDefaultCulture();
4040
}
@@ -73,15 +73,7 @@ public static CultureInfo CurrentCulture
7373
return ci;
7474
}
7575

76-
// if s_userDefaultCulture == null means CultureInfo statics didn't get initialized yet. this can happen if there early static
77-
// method get executed which eventually hit the cultureInfo code while CultureInfo statics didn’t get chance to initialize
78-
if (s_userDefaultCulture == null)
79-
{
80-
Init();
81-
}
82-
83-
Debug.Assert(s_userDefaultCulture != null);
84-
return s_userDefaultCulture;
76+
return s_userDefaultCulture ?? InitializeUserDefaultCulture();
8577
}
8678

8779
set

src/mscorlib/src/System/Globalization/CultureInfo.Windows.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ internal static CultureInfo GetUserDefaultCulture()
7272
return temp;
7373
}
7474

75-
private static CultureInfo GetUserDefaultUILanguage()
75+
private static CultureInfo GetUserDefaultUICulture()
7676
{
7777
if (GlobalizationMode.Invariant)
7878
return CultureInfo.InvariantCulture;
@@ -150,15 +150,7 @@ public static CultureInfo CurrentCulture
150150
return ci;
151151
}
152152

153-
// if s_userDefaultCulture == null means CultureInfo statics didn't get initialized yet. this can happen if there early static
154-
// method get executed which eventually hit the cultureInfo code while CultureInfo statics didn’t get chance to initialize
155-
if (s_userDefaultCulture == null)
156-
{
157-
Init();
158-
}
159-
160-
Debug.Assert(s_userDefaultCulture != null);
161-
return s_userDefaultCulture;
153+
return s_userDefaultCulture ?? InitializeUserDefaultCulture();
162154
}
163155

164156
set

src/mscorlib/src/System/Globalization/CultureInfo.cs

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,16 @@ internal static void AsyncLocalSetCurrentUICulture(AsyncLocalValueChangedArgs<Cu
148148
internal const int LOCALE_CUSTOM_DEFAULT = 0x0c00;
149149
internal const int LOCALE_INVARIANT = 0x007F;
150150

151-
//
152-
// The CultureData instance that reads the data provided by our CultureData class.
153-
//
154-
// Using a field initializer rather than a static constructor so that the whole class can be lazy
155-
// init.
156-
private static readonly bool init = Init();
157-
private static bool Init()
151+
private static CultureInfo InitializeUserDefaultCulture()
158152
{
159-
s_userDefaultCulture = GetUserDefaultCulture();
160-
s_userDefaultUICulture = GetUserDefaultUILanguage();
161-
return true;
153+
Interlocked.CompareExchange(ref s_userDefaultCulture, GetUserDefaultCulture(), null);
154+
return s_userDefaultCulture;
155+
}
156+
157+
private static CultureInfo InitializeUserDefaultUICulture()
158+
{
159+
Interlocked.CompareExchange(ref s_userDefaultUICulture, GetUserDefaultUICulture(), null);
160+
return s_userDefaultUICulture;
162161
}
163162

164163
////////////////////////////////////////////////////////////////////////
@@ -167,7 +166,6 @@ private static bool Init()
167166
//
168167
////////////////////////////////////////////////////////////////////////
169168

170-
171169
public CultureInfo(String name)
172170
: this(name, true)
173171
{
@@ -423,34 +421,9 @@ internal static CultureInfo GetCurrentUICultureNoAppX()
423421
return UserDefaultUICulture;
424422
}
425423

426-
internal static CultureInfo UserDefaultUICulture
427-
{
428-
get
429-
{
430-
// if s_userDefaultUICulture == null means CultureInfo statics didn't get initialized yet. this can happen if there early static
431-
// method get executed which eventually hit the cultureInfo code while CultureInfo statics didn’t get chance to initialize
432-
if (s_userDefaultUICulture == null)
433-
{
434-
Init();
435-
}
436-
437-
Debug.Assert(s_userDefaultUICulture != null);
438-
return s_userDefaultUICulture;
439-
}
440-
}
424+
internal static CultureInfo UserDefaultUICulture => s_userDefaultUICulture ?? InitializeUserDefaultUICulture();
441425

442-
public static CultureInfo InstalledUICulture
443-
{
444-
get
445-
{
446-
if (s_userDefaultCulture == null)
447-
{
448-
Init();
449-
}
450-
Debug.Assert(s_userDefaultCulture != null, "[CultureInfo.InstalledUICulture] s_userDefaultCulture != null");
451-
return s_userDefaultCulture;
452-
}
453-
}
426+
public static CultureInfo InstalledUICulture => s_userDefaultCulture ?? InitializeUserDefaultCulture();
454427

455428
public static CultureInfo DefaultThreadCurrentCulture
456429
{
@@ -920,7 +893,9 @@ public virtual DateTimeFormatInfo DateTimeFormat
920893

921894
public void ClearCachedData()
922895
{
923-
Init(); // reset the default culture values
896+
// reset the default culture values
897+
s_userDefaultCulture = GetUserDefaultCulture();
898+
s_userDefaultUICulture = GetUserDefaultUICulture();
924899

925900
RegionInfo.s_currentRegionInfo = null;
926901
#pragma warning disable 0618 // disable the obsolete warning

0 commit comments

Comments
 (0)