From fadbfaca8eed8ca4596526912dc92e80849cdb0e Mon Sep 17 00:00:00 2001 From: Meri Khamoyan Date: Tue, 25 Jun 2024 11:35:10 +0200 Subject: [PATCH] Make ChangeLocalTimeZone thread-safe --- .../System/TimeZoneInfoTests.cs | 57 +++++++++++-------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/TimeZoneInfoTests.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/TimeZoneInfoTests.cs index 16818b51ab89c..9caf917b9f1ba 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/TimeZoneInfoTests.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/TimeZoneInfoTests.cs @@ -3060,7 +3060,7 @@ public static void TestTimeZoneIdBackwardCompatibility(string oldId, string curr // Note we cannot test the DisplayName, as it will contain the ID. } - [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] + [Theory] [PlatformSpecific(TestPlatforms.Browser)] [InlineData("America/Buenos_Aires")] [InlineData("America/Catamarca")] @@ -3071,38 +3071,45 @@ public static void TestTimeZoneIdBackwardCompatibility(string oldId, string curr public static void ChangeLocalTimeZone(string id) { string originalTZ = Environment.GetEnvironmentVariable("TZ"); - try + object lockObject = new object(); + lock (lockObject) { - TimeZoneInfo.ClearCachedData(); - Environment.SetEnvironmentVariable("TZ", id); + try + { + TimeZoneInfo.ClearCachedData(); + Environment.SetEnvironmentVariable("TZ", id); - TimeZoneInfo localtz = TimeZoneInfo.Local; - TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(id); + TimeZoneInfo localtz = TimeZoneInfo.Local; + TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(id); - Assert.Equal(tz.StandardName, localtz.StandardName); - Assert.Equal(tz.DisplayName, localtz.DisplayName); - } - finally - { - TimeZoneInfo.ClearCachedData(); - Environment.SetEnvironmentVariable("TZ", originalTZ); + Assert.Equal(tz.StandardName, localtz.StandardName); + Assert.Equal(tz.DisplayName, localtz.DisplayName); + } + finally + { + TimeZoneInfo.ClearCachedData(); + Environment.SetEnvironmentVariable("TZ", originalTZ); + } } - try + lock (lockObject) { - TimeZoneInfo.ClearCachedData(); - Environment.SetEnvironmentVariable("TZ", id); + try + { + TimeZoneInfo.ClearCachedData(); + Environment.SetEnvironmentVariable("TZ", id); - TimeZoneInfo localtz = TimeZoneInfo.Local; - Assert.True(TimeZoneInfo.TryFindSystemTimeZoneById(id, out TimeZoneInfo tz)); + TimeZoneInfo localtz = TimeZoneInfo.Local; + Assert.True(TimeZoneInfo.TryFindSystemTimeZoneById(id, out TimeZoneInfo tz)); - Assert.Equal(tz.StandardName, localtz.StandardName); - Assert.Equal(tz.DisplayName, localtz.DisplayName); - } - finally - { - TimeZoneInfo.ClearCachedData(); - Environment.SetEnvironmentVariable("TZ", originalTZ); + Assert.Equal(tz.StandardName, localtz.StandardName); + Assert.Equal(tz.DisplayName, localtz.DisplayName); + } + finally + { + TimeZoneInfo.ClearCachedData(); + Environment.SetEnvironmentVariable("TZ", originalTZ); + } } }