Skip to content

Commit

Permalink
ConcurrencyUtilities: enable deleting of lock files on non-Windows pl…
Browse files Browse the repository at this point in the history
…atforms.
  • Loading branch information
tmds committed Nov 25, 2021
1 parent 9187a50 commit 13e2265
Show file tree
Hide file tree
Showing 2 changed files with 502 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/NuGet.Core/NuGet.Common/ConcurrencyUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public static class ConcurrencyUtilities
private static readonly TimeSpan SleepDuration = TimeSpan.FromMilliseconds(10);
private static readonly KeyedLock PerFileLock = new KeyedLock();

// FileOptions.DeleteOnClose causes concurrency issues on Mac OS X and Linux.
// These are fixed in .NET 7 (https://github.com/dotnet/runtime/pull/55327).
// To continue working in parallel with older versions of .NET,
// we cannot use DeleteOnClose by default until .NET 6 goes EOL (Nov 2024).
private static bool UseDeleteOnClose = RuntimeEnvironmentHelper.IsWindows ||
Environment.GetEnvironmentVariable("NUGET_ConcurrencyUtils_DeleteOnClose") == '1'; // opt-in.

public async static Task<T> ExecuteWithFileLockedAsync<T>(string filePath,
Func<CancellationToken, Task<T>> action,
CancellationToken token)
Expand Down Expand Up @@ -199,26 +206,14 @@ public static void ExecuteWithFileLocked(string filePath,

private static FileStream AcquireFileStream(string lockPath)
{
FileOptions options;
if (RuntimeEnvironmentHelper.IsWindows)
{
// This file is deleted when the stream is closed.
options = FileOptions.DeleteOnClose;
}
else
{
// FileOptions.DeleteOnClose causes concurrency issues on Mac OS X and Linux.
options = FileOptions.None;
}

// Sync operations have shown much better performance than FileOptions.Asynchronous
return new FileStream(
lockPath,
FileMode.OpenOrCreate,
FileAccess.ReadWrite,
FileShare.None,
bufferSize: 32,
options: options);
options: UseDeleteOnClose ? FileOptions.DeleteOnClose : FileOptions.None);
}

private static string _basePath;
Expand Down
Loading

0 comments on commit 13e2265

Please sign in to comment.