Skip to content

Commit

Permalink
Enable file delete using NUGET_ConcurrencyUtils_DeleteOnClose
Browse files Browse the repository at this point in the history
  • Loading branch information
tmds committed Jul 1, 2021
1 parent e33081a commit ea0ce96
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/NuGet.Core/NuGet.Common/ConcurrencyUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static class ConcurrencyUtilities
private static readonly TimeSpan SleepDuration = TimeSpan.FromMilliseconds(10);
private static Mutex GlobalMutex;
private static bool UseDeleteOnClose;
private static bool ManualDeleteOnClose;
private static readonly KeyedLock PerFileLock = new KeyedLock();

static ConcurrencyUtilities()
Expand Down Expand Up @@ -48,7 +49,16 @@ static ConcurrencyUtilities()
UseDeleteOnClose = RuntimeEnvironmentHelper.IsWindows;
if (!UseDeleteOnClose)
{
GlobalMutex = new Mutex(false, "Global\\NuGet.Common.ConcurrencyUtilities");
// We need to remain compatible with older SDKS that are still supported.
// That means we can really remove lock files when .NET Core 3.1 is EOL on December 3, 2022.
// The removing can already be enabled by setting 'NUGET_ConcurrencyUtils_DeleteOnClose' to '1'.
// Setting it to 'NoMutex' allows to opt-out of using the GlobalMutex.
string NUGET_ConcurrencyUtils_DeleteOnClose = Environment.GetEnvironmentVariable("NUGET_ConcurrencyUtils_DeleteOnClose");
if (NUGET_ConcurrencyUtils_DeleteOnClose != "NoMutex")
{
GlobalMutex = new Mutex(false, "Global\\NuGet.ConcurrencyUtilities.DeleteOnClose");
ManualDeleteOnClose = NUGET_ConcurrencyUtils_DeleteOnClose == "1";
}
}
}

Expand Down Expand Up @@ -133,7 +143,7 @@ public async static Task<T> ExecuteWithFileLockedAsync<T>(string filePath,
EnterGlobalMutex();
try
{
if (!UseDeleteOnClose)
if (ManualDeleteOnClose)
{
File.Delete(fs.Name);
}
Expand Down Expand Up @@ -234,7 +244,7 @@ public static void ExecuteWithFileLocked(string filePath,
EnterGlobalMutex();
try
{
if (!UseDeleteOnClose)
if (ManualDeleteOnClose)
{
File.Delete(fs.Name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public class SynchronizationTests
private int _value2 = 0;
private SemaphoreSlim _waitForEverStarted = new SemaphoreSlim(0, 1);

public SynchronizationTests()
{
// Delete lock files also on non-Windows platforms.
Environment.SetEnvironmentVariable("NUGET_ConcurrencyUtils_DeleteOnClose", "1");
}

private readonly int DefaultTimeOut = (int)TimeSpan.FromMinutes(5).TotalMilliseconds;

[Fact]
Expand Down

0 comments on commit ea0ce96

Please sign in to comment.