Skip to content

Commit

Permalink
Makes path resolution thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Aug 18, 2024
1 parent 6f02db6 commit 3692155
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 6 additions & 2 deletions CSharpInteractive.HostApi/DockerRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,13 @@ public IStartInfo GetStartInfo(IHost host)
if (host == null) throw new ArgumentNullException(nameof(host));
var directoryMap = new Dictionary<string, string>();
var pathResolver = new PathResolver(Platform, directoryMap);
using var pathResolverToken = host.GetService<HostComponents>().PathResolverContext.Register(pathResolver);
IStartInfo startInfo;
using (host.GetService<HostComponents>().PathResolverContext.Register(pathResolver))
{
startInfo = CommandLine.GetStartInfo(host);
}

var settings = host.GetService<HostComponents>().DockerSettings;
var startInfo = CommandLine.GetStartInfo(host);
var cmd = new CommandLine(string.IsNullOrWhiteSpace(ExecutablePath) ? settings.DockerExecutablePath : ExecutablePath)
.WithShortName(!string.IsNullOrWhiteSpace(ShortName) ? ShortName : $"{startInfo.ShortName} in the docker container {Image}")
.WithWorkingDirectory(WorkingDirectory)
Expand Down
4 changes: 3 additions & 1 deletion CSharpInteractive/Core/PathResolverContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ namespace CSharpInteractive.Core;

internal class PathResolverContext(IHost host) : IPathResolverContext, IVirtualContext
{
private readonly object _lockObject = new();
private IPathResolver _currentResolver = EmptyResolver.Shared;
private IPathResolver _prevResolver = EmptyResolver.Shared;

public bool IsActive => _currentResolver != EmptyResolver.Shared;

public IDisposable Register(IPathResolver resolver)
{
Monitor.Enter(_lockObject);
var prevResolver = _prevResolver;
_prevResolver = _currentResolver;
_currentResolver = resolver;
return Disposable.Create(() =>
{
_currentResolver = _prevResolver;
_prevResolver = prevResolver;
Monitor.Exit(_lockObject);
});
}

Expand Down

0 comments on commit 3692155

Please sign in to comment.