Skip to content

Commit 6ca00a9

Browse files
committed
Add lock
1 parent c612cb7 commit 6ca00a9

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/Microsoft.TemplateEngine.Edge/Constraints/WorkloadConstraintFactory.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace Microsoft.TemplateEngine.Edge.Constraints
1111
{
1212
public sealed class WorkloadConstraintFactory : ITemplateConstraintFactory
1313
{
14+
private static readonly SemaphoreSlim Mutex = new(1);
15+
1416
Guid IIdentifiedComponent.Id { get; } = Guid.Parse("{F8BA5B13-7BD6-47C8-838C-66626526817B}");
1517

1618
string ITemplateConstraintFactory.Type => "workload";
@@ -97,8 +99,17 @@ private static IEnumerable<string> ParseArgs(string? args)
9799
}
98100

99101
token.ThrowIfCancellationRequested();
100-
IEnumerable<WorkloadInfo> currentProviderWorkloads = await providers[0].GetInstalledWorkloadsAsync(token).ConfigureAwait(false);
101-
workloads = currentProviderWorkloads.ToList();
102+
103+
await Mutex.WaitAsync(token).ConfigureAwait(false);
104+
try
105+
{
106+
IEnumerable<WorkloadInfo> currentProviderWorkloads = await providers[0].GetInstalledWorkloadsAsync(token).ConfigureAwait(false);
107+
workloads = currentProviderWorkloads.ToList();
108+
}
109+
finally
110+
{
111+
Mutex.Release();
112+
}
102113

103114
if (workloads.Select(w => w.Id).HasDuplicates(StringComparer.InvariantCultureIgnoreCase))
104115
{

0 commit comments

Comments
 (0)