<commit_before>using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Framework.DesignTimeHost.Models;
using Microsoft.Framework.Logging;
using OmniSharp.Models;
using OmniSharp.Services;

namespace OmniSharp.AspNet5
{
    public class PackagesRestoreTool
    {
        private readonly ILogger _logger;
        private readonly IEventEmitter _emitter;
        private readonly AspNet5Context _context;
        private readonly AspNet5Paths _paths;
        private readonly object _lock;
        private readonly IDictionary<string, object> _projectLocks;
        private readonly SemaphoreSlim _semaphore;

        public PackagesRestoreTool(ILoggerFactory logger, IEventEmitter emitter, AspNet5Context context, AspNet5Paths paths)
        {
            _logger = logger.Create<PackagesRestoreTool>();
            _emitter = emitter;
            _context = context;
            _paths = paths;
            _lock = new object();
            _projectLocks = new Dictionary<string, object>();
            _semaphore = new SemaphoreSlim(Environment.ProcessorCount / 2);
        }

        public void Run(Project project)
        {
            Task.Factory.StartNew(() =>
            {
                object projectLock;
                lock (_lock)
                {
                    if (!_projectLocks.TryGetValue(project.Path, out projectLock))
                    {
                        projectLock = new object();
                        _projectLocks.Add(project.Path, projectLock);
                    }
                }

                lock (projectLock)
                {
                    _emitter.Emit(EventTypes.PackageRestoreStarted, new PackageRestoreMessage() { FileName = project.Path });
                    _semaphore.Wait();
                    try
                    {
                        var psi = new ProcessStartInfo()
                        {
                            FileName = _paths.Dnu ?? _paths.Kpm,
                            WorkingDirectory = Path.GetDirectoryName(project.Path),
                            CreateNoWindow = true,
                            UseShellExecute = false,
                            RedirectStandardOutput = true,
                            RedirectStandardError = true,
                            Arguments = "restore"
                        };

                        _logger.WriteInformation("restore packages {0} {1} for {2}", psi.FileName, psi.Arguments, psi.WorkingDirectory);

                        var restoreProcess = Process.Start(psi);
                        if (restoreProcess.HasExited)
                        {
                            _logger.WriteError("restore command ({0}) failed with error code {1}", psi.FileName, restoreProcess.ExitCode);
                        }
                        else
                        {
                            restoreProcess.OutputDataReceived += (sender, e) => _logger.WriteInformation(e.Data);
                            restoreProcess.ErrorDataReceived += (sender, e) => _logger.WriteError(e.Data);
                            restoreProcess.BeginOutputReadLine();
                            restoreProcess.BeginErrorReadLine();
                            restoreProcess.WaitForExit();

                            _context.Connection.Post(new Message()
                            {
                                ContextId = project.ContextId,
                                MessageType = "RestoreComplete",
                                HostId = _context.HostId
                            });
                            _emitter.Emit(EventTypes.PackageRestoreFinished, new PackageRestoreMessage()
                            {
                                FileName = project.Path,
                                Succeeded = restoreProcess.ExitCode == 0
                            });
                        }
                    }
                    finally
                    {
                        _semaphore.Release();
                        _projectLocks.Remove(project.Path);
                    }
                }
            });
        }
    }
}<commit_msg>add a watchdog that kill dnu|kpm restore if no process aka output is reported for >20 seconds. try one more time<commit_after>using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Framework.DesignTimeHost.Models;
using Microsoft.Framework.Logging;
using OmniSharp.Models;
using OmniSharp.Services;

namespace OmniSharp.AspNet5
{
    public class PackagesRestoreTool
    {
        private readonly ILogger _logger;
        private readonly IEventEmitter _emitter;
        private readonly AspNet5Context _context;
        private readonly AspNet5Paths _paths;
        private readonly object _lock;
        private readonly IDictionary<string, object> _projectLocks;
        private readonly SemaphoreSlim _semaphore;

        public PackagesRestoreTool(ILoggerFactory logger, IEventEmitter emitter, AspNet5Context context, AspNet5Paths paths)
        {
            _logger = logger.Create<PackagesRestoreTool>();
            _emitter = emitter;
            _context = context;
            _paths = paths;
            _lock = new object();
            _projectLocks = new Dictionary<string, object>();
            _semaphore = new SemaphoreSlim(Environment.ProcessorCount / 2);
        }

        public void Run(Project project)
        {
            Task.Factory.StartNew(() =>
            {
                object projectLock;
                lock (_lock)
                {
                    if (!_projectLocks.TryGetValue(project.Path, out projectLock))
                    {
                        projectLock = new object();
                        _projectLocks.Add(project.Path, projectLock);
                    }
                }

                lock (projectLock)
                {
                    var exitCode = -1;
                    _emitter.Emit(EventTypes.PackageRestoreStarted, new PackageRestoreMessage() { FileName = project.Path });
                    _semaphore.Wait();
                    try
                    {
                        exitCode = DoRun(project, 1);

                        _context.Connection.Post(new Message()
                        {
                            ContextId = project.ContextId,
                            MessageType = "RestoreComplete",
                            HostId = _context.HostId
                        });
                    }
                    finally
                    {
                        _semaphore.Release();
                        _projectLocks.Remove(project.Path);
                        _emitter.Emit(EventTypes.PackageRestoreFinished, new PackageRestoreMessage()
                        {
                            FileName = project.Path,
                            Succeeded = exitCode == 0
                        });
                    }
                }
            });
        }

        private int DoRun(Project project, int retry)
        {
            var psi = new ProcessStartInfo()
            {
                FileName = _paths.Dnu ?? _paths.Kpm,
                WorkingDirectory = Path.GetDirectoryName(project.Path),
                CreateNoWindow = true,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                Arguments = "restore"
            };

            _logger.WriteInformation("restore packages {0} {1} for {2}", psi.FileName, psi.Arguments, psi.WorkingDirectory);

            var restoreProcess = Process.Start(psi);
            if (restoreProcess.HasExited)
            {
                _logger.WriteError("restore command ({0}) failed with error code {1}", psi.FileName, restoreProcess.ExitCode);
                return restoreProcess.ExitCode;
            }

            // watch dog to workaround dnu restore hangs
            var lastSignal = DateTime.UtcNow;
            var wasKilledByWatchDog = false;
            var watchDog = Task.Factory.StartNew(async () =>
            {
                while (!restoreProcess.HasExited)
                {
                    if (DateTime.UtcNow - lastSignal > TimeSpan.FromSeconds(20))
                    {
                        _logger.WriteError("killing restore comment ({0}) because it seems be stuck. retrying {1} more time(s)...", restoreProcess.Id, retry);
                        wasKilledByWatchDog = true;
                        restoreProcess.KillAll();
                    }
                    await Task.Delay(TimeSpan.FromSeconds(10));
                }
            });

            restoreProcess.OutputDataReceived += (sender, e) =>
            {
                _logger.WriteInformation(e.Data);
                lastSignal = DateTime.UtcNow;
            };
            restoreProcess.ErrorDataReceived += (sender, e) =>
            {
                _logger.WriteError(e.Data);
                lastSignal = DateTime.UtcNow;
            };

            restoreProcess.BeginOutputReadLine();
            restoreProcess.BeginErrorReadLine();
            restoreProcess.WaitForExit();

            return wasKilledByWatchDog && retry > 0
                ? DoRun(project, retry - 1)
                : restoreProcess.ExitCode;
        }
    }
}
{'fraction_non_alphanumeric': 0.07441321966108831, 'fraction_numerical': 0.003789074834227976, 'mean_word_length': 1.297944377267231, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '123944', 'n_tokens_mistral': 2249, 'n_tokens_neox': 2100, 'n_words': 453}