Skip to content

Commit ecdd772

Browse files
authored
TestFramework, env proble, use double-check-lock instead of GetOrAdd which isn't atomic. (Azure#20664)
* Use double-check-lock instead of GetOrAdd which isn't atomic. * some pr feedback. * dictionary. * pr feedback.
1 parent 56a120b commit ecdd772

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

sdk/core/Azure.Core.TestFramework/src/TestEnvironment.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
using System.IO;
88
using System.Reflection;
99
using System.Text.Json;
10-
using System.Threading;
1110
using System.Threading.Tasks;
1211
using Azure.Identity;
1312
using System.ComponentModel;
1413
using System.Linq;
1514
using NUnit.Framework;
16-
using System.Collections.Concurrent;
1715

1816
namespace Azure.Core.TestFramework
1917
{
@@ -26,7 +24,7 @@ public abstract class TestEnvironment
2624
[EditorBrowsableAttribute(EditorBrowsableState.Never)]
2725
public static string RepositoryRoot { get; }
2826

29-
private static readonly ConcurrentDictionary<Type, Task> s_environmentStateCache = new ConcurrentDictionary<Type, Task>();
27+
private static readonly Dictionary<Type, Task> s_environmentStateCache = new Dictionary<Type, Task>();
3028

3129
private readonly string _prefix;
3230

@@ -201,7 +199,16 @@ public async ValueTask WaitForEnvironmentAsync()
201199
{
202200
if (GlobalIsRunningInCI && Mode == RecordedTestMode.Live)
203201
{
204-
await s_environmentStateCache.GetOrAdd(GetType(), t => WaitForEnvironmentInternalAsync());
202+
Task task;
203+
lock (s_environmentStateCache)
204+
{
205+
if (!s_environmentStateCache.TryGetValue(GetType(), out task))
206+
{
207+
task = WaitForEnvironmentInternalAsync();
208+
s_environmentStateCache[GetType()] = task;
209+
}
210+
}
211+
await task;
205212
}
206213
}
207214

0 commit comments

Comments
 (0)