Skip to content

Commit

Permalink
Delete lock files on non-Windows platforms. (#4123)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmds authored Jan 14, 2022
1 parent 42da1b2 commit b56eacc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ then
fi

# restore packages
echo "dotnet msbuild build/bootstrap.proj /t:Restore"
dotnet msbuild build/bootstrap.proj /t:Restore
echo "dotnet msbuild build/build.proj /t:Restore /p:VisualStudioVersion=16.0 /p:Configuration=Release /p:BuildNumber=1 /p:ReleaseLabel=beta"
dotnet msbuild build/build.proj /t:Restore /p:VisualStudioVersion=16.0 /p:Configuration=Release /p:BuildNumber=1 /p:ReleaseLabel=beta

Expand Down
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

0 comments on commit b56eacc

Please sign in to comment.