From 4ada52dc5e8a8f17534946efb7dd1195c0e69f27 Mon Sep 17 00:00:00 2001 From: "Eric St. John" Date: Tue, 21 Apr 2015 16:31:07 -0700 Subject: [PATCH] Prevent concurrent restore of same project.json DNX has an issue with concurrent restore: https://github.com/aspnet/dnx/issues/1682 Previously I would workaround by copying to intermediate, but this breaks in official build where intermediate is not under the scope of our nuget.config, so custom package sources are ignored. Fix this instead by wrapping the call to dnu restore with a mutex unique to the project.json name. We only need to do this for the test project.json because that is the only one we'll restore concurrently. --- .../ExecWithMutex.cs | 44 +++++++++++++++++++ .../Microsoft.DotNet.Build.Tasks.csproj | 2 + .../Targets/publishtest.targets | 12 ++--- 3 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 src/Microsoft.DotNet.Build.Tasks/ExecWithMutex.cs diff --git a/src/Microsoft.DotNet.Build.Tasks/ExecWithMutex.cs b/src/Microsoft.DotNet.Build.Tasks/ExecWithMutex.cs new file mode 100644 index 0000000000..9f6d4f2565 --- /dev/null +++ b/src/Microsoft.DotNet.Build.Tasks/ExecWithMutex.cs @@ -0,0 +1,44 @@ +using Microsoft.Build.Framework; +using Microsoft.Build.Tasks; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace Microsoft.DotNet.Build.Tasks +{ + public class ExecWithMutex : Exec + { + [Required] + public string MutexName + { + get; + set; + } + + protected override int ExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands) + { + string actualMutexName = MutexName.Replace(Path.DirectorySeparatorChar, '_'); + bool created = false; + + using (Mutex mutex = new Mutex(true, actualMutexName, out created)) + { + try + { + if (!created) + { + mutex.WaitOne(); + } + return base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands); + } + finally + { + mutex.ReleaseMutex(); + } + } + } + } +} diff --git a/src/Microsoft.DotNet.Build.Tasks/Microsoft.DotNet.Build.Tasks.csproj b/src/Microsoft.DotNet.Build.Tasks/Microsoft.DotNet.Build.Tasks.csproj index 6a8e1b1039..b33ef91954 100644 --- a/src/Microsoft.DotNet.Build.Tasks/Microsoft.DotNet.Build.Tasks.csproj +++ b/src/Microsoft.DotNet.Build.Tasks/Microsoft.DotNet.Build.Tasks.csproj @@ -23,6 +23,7 @@ + @@ -41,6 +42,7 @@ + diff --git a/src/Microsoft.DotNet.Build.Tasks/Targets/publishtest.targets b/src/Microsoft.DotNet.Build.Tasks/Targets/publishtest.targets index 8f10779eee..e9c0146228 100644 --- a/src/Microsoft.DotNet.Build.Tasks/Targets/publishtest.targets +++ b/src/Microsoft.DotNet.Build.Tasks/Targets/publishtest.targets @@ -1,6 +1,7 @@ + @@ -20,18 +21,11 @@ - - - - - + - +