Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only DeleteOnClose when running under Windows for global lock #431

Merged
merged 1 commit into from
Mar 30, 2016
Merged
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
16 changes: 14 additions & 2 deletions src/NuGet.Core/NuGet.Common/ConcurrencyUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,27 @@ public async static Task<T> ExecuteWithFileLockedAsync<T>(string filePath,
{
try
{
// This file is deleted when the stream is closed.
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
fs = new FileStream(
lockPath,
FileMode.OpenOrCreate,
FileAccess.ReadWrite,
FileShare.None,
bufferSize: 32,
options: FileOptions.DeleteOnClose);
options: options);
}
catch (DirectoryNotFoundException)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using NuGet.Test.Utility;

namespace NuGet.Common.Test
{
Expand All @@ -13,37 +15,57 @@ public class ConcurrencyUtilitiesTests
public async Task ConcurrencyUtilities_LockStress()
{
// Arrange
var sem = new ManualResetEventSlim();
var threads = 1000;
var tasks = new Stack<Task<bool>>(threads);
var path = Path.Combine(Path.GetTempPath(), "ConcurrencyUtilities_LockStress");
var token = CancellationToken.None;
Func<CancellationToken, Task<bool>> action = (ct) => {
// Wait till all threads are ready
sem.Wait();
return Task.FromResult(true);
};

while (tasks.Count < threads)
{
var task = Task.Run(async () => await ConcurrencyUtilities.ExecuteWithFileLockedAsync<bool>(
path,
action,
token));

tasks.Push(task);
}

// Act
// Release all the threads at once
sem.Set();
await Task.WhenAll(tasks);

// Assert
while (tasks.Count > 0)
using(var testDirectory = TestFileSystemUtility.CreateRandomTestFolder())
{
// Verify everything finished without errors
Assert.True(await tasks.Pop());
// This is the path that uniquely identifies the system-wide mutex.
var path = Path.Combine(testDirectory, "ConcurrencyUtilities_LockStress_Verification");

// This is a semaphore use to verify the lock.
var verificationSemaphore = new SemaphoreSlim(1);

// Iterate a lot, to increase confidence.
const int threads = 50;
const int iterations = 10;

// This is the action that is execute inside of the lock.
Func<CancellationToken, Task<bool>> lockedActionAsync = async lockedToken =>
{
var acquired = await verificationSemaphore.WaitAsync(0);
if (!acquired)
{
return false;
}

// Hold the lock for a little bit.
await Task.Delay(TimeSpan.FromMilliseconds(1));

verificationSemaphore.Release();
return true;
};

// Loop the same action, over and over.
Func<int, Task<List<bool>>> loopAsync = async thread =>
{
var loopResults = new List<bool>();
foreach (var iteration in Enumerable.Range(0, iterations))
{
var result = await ConcurrencyUtilities.ExecuteWithFileLockedAsync(
path,
lockedActionAsync,
CancellationToken.None);
loopResults.Add(result);
}

return loopResults;
};

// Act
var tasks = Enumerable.Range(0, threads).Select(loopAsync);
var results = (await Task.WhenAll(tasks)).SelectMany(r => r).ToArray();

// Assert
Assert.Equal(threads * iterations, results.Length);
Assert.DoesNotContain(false, results);
}
}

Expand Down