Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/Microsoft.DotNet.Build.Tasks.Workloads/src/GenerateMsiBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ public string SuppressIces
/// </summary>
private readonly object extractNuGetLock = new object();

/// <summary>
/// Used to syncrhonize access to MSI task items.
/// </summary>
private readonly object msiTaskItemsLock = new();

/// <summary>
/// Generate a set of MSIs for the specified platforms using the specified NuGet package.
/// </summary>
Expand All @@ -124,11 +129,16 @@ public string SuppressIces
protected IEnumerable<ITaskItem> Generate(string sourcePackage, string swixPackageId, string outputPath, WorkloadPackKind kind, params string[] platforms)
{
NugetPackage nupkg = null;
List<TaskItem> msis = null;

lock (extractNuGetLock)
{
nupkg = new(sourcePackage, Log);
}
List<TaskItem> msis = new();
lock (msiTaskItemsLock)
{
msis = new();
}

// MSI ProductName defaults to the package title and fallback to the package ID with a warning.
string productName = nupkg.Title;
Expand Down Expand Up @@ -310,7 +320,10 @@ protected IEnumerable<ITaskItem> Generate(string sourcePackage, string swixPacka
// Generate a .csproj to build a NuGet payload package to carry the MSI and JSON manifest
msi.SetMetadata(Metadata.PackageProject, GeneratePackageProject(msi.ItemSpec, msiJsonPath, platform, nupkg));

msis.Add(msi);
lock (msiTaskItemsLock)
{
msis.Add(msi);
}
});

return msis;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,20 @@ public ITaskItem[] MissingPacks
set;
}

/// <summary>
/// Used for synchronizing on MSI task item.
/// </summary>
private readonly object msiTaskItemsLock = new();

public override bool Execute()
{
try
{
List<ITaskItem> msis = new();
List<ITaskItem> msis = null;
lock (msiTaskItemsLock)
{
msis = new();
}
List<ITaskItem> missingPacks = new();

if (string.IsNullOrWhiteSpace(PackagesPath))
Expand Down Expand Up @@ -96,7 +105,12 @@ public override bool Execute()

System.Threading.Tasks.Parallel.ForEach(packsToGenerate, p =>
{
msis.AddRange(Generate(p.sourcePackage, p.swixPackageId, p.outputPath, p.kind, p.platforms));
var msiItems = Generate(p.sourcePackage, p.swixPackageId, p.outputPath, p.kind, p.platforms);

lock (msiTaskItemsLock)
{
msis.AddRange(msiItems);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And on line 116 for completeness? Usually easier to just lock all access to an object rather than rationalize which.

});

Msis = msis.ToArray();
Expand Down