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

Disallow concurrent RestoreAsync on same working directories. #783

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AsyncKeyedLock" Version="6.3.4" />
<PackageReference Include="Avalonia" />
<PackageReference Include="Mono.Posix.NETStandard" Version="1.0.0" />
<PackageReference Include="Newtonsoft.Json" />
Expand Down Expand Up @@ -57,4 +58,4 @@
</EmbeddedResource>
</ItemGroup>

</Project>
</Project>
26 changes: 11 additions & 15 deletions AvalonStudio/AvalonStudio.Extensibility/Projects/DotnetCLI.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using AvalonStudio.CommandLineTools;
using AsyncKeyedLock;
using AvalonStudio.CommandLineTools;
using AvalonStudio.Extensibility.Utils;
using AvalonStudio.Utils;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand All @@ -13,8 +13,7 @@ namespace AvalonStudio.Extensibility.Projects
{
public class DotNetCliService
{
private readonly ConcurrentDictionary<string, object> _locks;
private readonly SemaphoreSlim _semaphore;
private readonly AsyncKeyedLocker<string> _locks;
private readonly IConsole _console;
private DotNetInfo _info;

Expand All @@ -27,9 +26,12 @@ public class DotNetCliService
public DotNetInfo Info => _info;

private DotNetCliService(string dotnetPath = null)
{
this._locks = new ConcurrentDictionary<string, object>();
this._semaphore = new SemaphoreSlim(Environment.ProcessorCount / 2);
{
this._locks = new AsyncKeyedLocker<string>(o =>
{
o.PoolSize = 20;
o.PoolInitialFill = 1;
});
this._console = IoC.Get<IConsole>();

if (dotnetPath != null)
Expand Down Expand Up @@ -72,16 +74,14 @@ public void SetDotNetPath(string path)

public Task RestoreAsync(string workingDirectory, string arguments = null, Action onFailure = null)
{
return Task.Factory.StartNew(() =>
return Task.Factory.StartNew(async () =>
{
_console.WriteLine($"Begin dotnet restore in '{workingDirectory}'");

var restoreLock = _locks.GetOrAdd(workingDirectory, new object());
lock (restoreLock)
using (await _locks.LockAsync(workingDirectory))
{
ShellExecuteResult? exitStatus = null;
//_eventEmitter.RestoreStarted(workingDirectory);
_semaphore.Wait();
try
{
// A successful restore will update the project lock file which is monitored
Expand All @@ -90,10 +90,6 @@ public Task RestoreAsync(string workingDirectory, string arguments = null, Actio
}
finally
{
_semaphore.Release();

_locks.TryRemove(workingDirectory, out _);

//_eventEmitter.RestoreFinished(workingDirectory, exitStatus.Succeeded);

if (exitStatus?.ExitCode != 0 && onFailure != null)
Expand Down
Loading