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
Changes from 1 commit
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
Next Next commit
Allow concurrent RestoreAsync on different working directories.
MarkCiliaVincenti committed Dec 21, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit e5883c0fe2dfd17710ad4d564a4b5e2de10adf7e
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AsyncKeyedLock" Version="6.2.4" />
<PackageReference Include="Avalonia" />
<PackageReference Include="Mono.Posix.NETStandard" Version="1.0.0" />
<PackageReference Include="Newtonsoft.Json" />
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;
@@ -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;

@@ -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)
@@ -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
@@ -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)