Skip to content
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
64 changes: 45 additions & 19 deletions src/BenchmarkDotNet/Extensions/ProcessExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using BenchmarkDotNet.Characteristics;
using BenchmarkDotNet.Detectors;
using BenchmarkDotNet.Engines;
Expand All @@ -13,6 +8,13 @@
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains.CoreRun;
using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;


namespace BenchmarkDotNet.Extensions
Expand Down Expand Up @@ -210,27 +212,51 @@ private static void GetAllChildIdsUnix(int parentId, HashSet<int> children, Time

private static (int exitCode, string output) RunProcessAndReadOutput(string fileName, string arguments, TimeSpan timeout)
{
var startInfo = new ProcessStartInfo
using var process = new Process
{
FileName = fileName,
Arguments = arguments,
RedirectStandardOutput = true,
UseShellExecute = false
StartInfo = new ProcessStartInfo
{
FileName = fileName,
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = false,
UseShellExecute = false
},
EnableRaisingEvents = true
};

using (var process = Process.Start(startInfo))
var stdout = new StringBuilder();

var tcsExited = new TaskCompletionSource<bool>();
var tcsStdout = new TaskCompletionSource<bool>();

process.Exited += (_, __) => tcsExited.TrySetResult(true);
process.OutputDataReceived += (_, e) =>
{
if (process.WaitForExit((int)timeout.TotalMilliseconds))
{
return (process.ExitCode, process.StandardOutput.ReadToEnd());
}
if (e.Data != null)
stdout.AppendLine(e.Data);
else
{
process.Kill();
}
tcsStdout.TrySetResult(true);
};

return (process.ExitCode, default);
process.Start();
process.BeginOutputReadLine();

var tasks = Task.WhenAll(tcsExited.Task, tcsStdout.Task);
if (tasks.Wait(timeout))
return (process.ExitCode, stdout.ToString());

// Handle timeout
try
{
process.KillTree();
}
catch
{
// Ignore exception
}

return (process.HasExited ? process.ExitCode : -1, default);
}

private static int RunProcessAndIgnoreOutput(string fileName, string arguments, TimeSpan timeout)
Expand Down
1 change: 0 additions & 1 deletion src/BenchmarkDotNet/Helpers/ProcessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ internal static class ProcessHelper
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
if (environmentVariables != null)
foreach (var variable in environmentVariables)
Expand Down
2 changes: 1 addition & 1 deletion src/BenchmarkDotNet/Validators/DotNetSdkValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ private static IEnumerable<Version> GetInstalledDotNetSdks(string? customDotNetC
return Enumerable.Empty<Version>();
}

var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();

if (process.ExitCode == 0)
{
var output = process.StandardOutput.ReadToEnd();
var lines = output.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

var versions = new List<Version>(lines.Count());
Expand Down