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
24 changes: 13 additions & 11 deletions src/Polyfill/Polyfill_Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,24 +533,26 @@ static int ToSignalNumber(PosixSignal signal)
}

static void WaitForExitOrThrow(Process target, TimeSpan? timeout)
{
if (!TryWaitForExit(target, timeout))
{
throw new TimeoutException("The process did not exit within the specified timeout.");
}
}

static bool TryWaitForExit(Process target, TimeSpan? timeout)
{
if (timeout is { } value)
{
var ms = (long)value.TotalMilliseconds;
if (ms < 0 || ms > int.MaxValue)
{
target.WaitForExit();
return;
}
if (!target.WaitForExit((int)ms))
if (ms >= 0 && ms <= int.MaxValue)
{
throw new TimeoutException("The process did not exit within the specified timeout.");
return target.WaitForExit((int)ms);
}
}
else
{
target.WaitForExit();
}

target.WaitForExit();
return true;
}
#endif
}
38 changes: 35 additions & 3 deletions src/Polyfill/ProcessPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ static partial class Polyfill
public static ProcessExitStatus Run(ProcessStartInfo startInfo, TimeSpan? timeout = default)
{
using var process = StartOrThrow(startInfo);
WaitForExitOrThrow(process, timeout);
if (!TryWaitForExit(process, timeout))
{
// as on net11, a process that outlives the timeout is killed and reported as canceled
KillAndWait(process);
return new(-1, canceled: true);
}

return ToExitStatus(process.ExitCode);
}

Expand All @@ -40,7 +46,15 @@ public static ProcessExitStatus Run(string fileName, IEnumerable<string>? argume
{
if (silent)
{
return Process.RunAndCaptureText(fileName, arguments, timeout).ExitStatus;
try
{
return Process.RunAndCaptureText(fileName, arguments, timeout).ExitStatus;
}
catch (TimeoutException)
{
// the process has been killed
return new(-1, canceled: true);
}
}

return Process.Run(BuildStartInfo(fileName, arguments), timeout);
Expand Down Expand Up @@ -114,7 +128,13 @@ public static ProcessTextOutput RunAndCaptureText(ProcessStartInfo startInfo, Ti
var stdoutTask = process.StandardOutput.ReadToEndAsync();
var stderrTask = process.StandardError.ReadToEndAsync();
var pid = process.Id;
WaitForExitOrThrow(process, timeout);
if (!TryWaitForExit(process, timeout))
{
// as on net11, a process that outlives the timeout is killed
KillAndWait(process);
throw new TimeoutException("The operation has timed out.");
}

var status = ToExitStatus(process.ExitCode);
return new(status, stdoutTask.GetAwaiter().GetResult(), stderrTask.GetAwaiter().GetResult(), pid);
}
Expand Down Expand Up @@ -270,6 +290,18 @@ static void ConfigureForCapture(ProcessStartInfo startInfo)
startInfo.UseShellExecute = false;
}

static void KillAndWait(Process process)
{
TryKill(process);
try
{
process.WaitForExit();
}
catch
{
}
}

static void TryKill(Process process)
{
try
Expand Down
22 changes: 11 additions & 11 deletions src/Split/net10.0/Polyfill_Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,23 +399,23 @@ static int ToSignalNumber(PosixSignal signal)
static extern int kill(int pid, int signal);
}
static void WaitForExitOrThrow(Process target, TimeSpan? timeout)
{
if (!TryWaitForExit(target, timeout))
{
throw new TimeoutException("The process did not exit within the specified timeout.");
}
}
static bool TryWaitForExit(Process target, TimeSpan? timeout)
{
if (timeout is { } value)
{
var ms = (long)value.TotalMilliseconds;
if (ms < 0 || ms > int.MaxValue)
{
target.WaitForExit();
return;
}
if (!target.WaitForExit((int)ms))
if (ms >= 0 && ms <= int.MaxValue)
{
throw new TimeoutException("The process did not exit within the specified timeout.");
return target.WaitForExit((int)ms);
}
}
else
{
target.WaitForExit();
}
target.WaitForExit();
return true;
}
}
32 changes: 29 additions & 3 deletions src/Split/net10.0/ProcessPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ static partial class Polyfill
public static ProcessExitStatus Run(ProcessStartInfo startInfo, TimeSpan? timeout = default)
{
using var process = StartOrThrow(startInfo);
WaitForExitOrThrow(process, timeout);
if (!TryWaitForExit(process, timeout))
{
KillAndWait(process);
return new(-1, canceled: true);
}
return ToExitStatus(process.ExitCode);
}
/// <summary>
Expand All @@ -34,7 +38,14 @@ public static ProcessExitStatus Run(string fileName, IEnumerable<string>? argume
{
if (silent)
{
return Process.RunAndCaptureText(fileName, arguments, timeout).ExitStatus;
try
{
return Process.RunAndCaptureText(fileName, arguments, timeout).ExitStatus;
}
catch (TimeoutException)
{
return new(-1, canceled: true);
}
}
return Process.Run(BuildStartInfo(fileName, arguments), timeout);
}
Expand Down Expand Up @@ -97,7 +108,11 @@ public static ProcessTextOutput RunAndCaptureText(ProcessStartInfo startInfo, Ti
var stdoutTask = process.StandardOutput.ReadToEndAsync();
var stderrTask = process.StandardError.ReadToEndAsync();
var pid = process.Id;
WaitForExitOrThrow(process, timeout);
if (!TryWaitForExit(process, timeout))
{
KillAndWait(process);
throw new TimeoutException("The operation has timed out.");
}
var status = ToExitStatus(process.ExitCode);
return new(status, stdoutTask.GetAwaiter().GetResult(), stderrTask.GetAwaiter().GetResult(), pid);
}
Expand Down Expand Up @@ -235,6 +250,17 @@ static void ConfigureForCapture(ProcessStartInfo startInfo)
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
}
static void KillAndWait(Process process)
{
TryKill(process);
try
{
process.WaitForExit();
}
catch
{
}
}
static void TryKill(Process process)
{
try
Expand Down
22 changes: 11 additions & 11 deletions src/Split/net461/Polyfill_Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,23 +445,23 @@ static int ToSignalNumber(PosixSignal signal)
static extern int kill(int pid, int signal);
}
static void WaitForExitOrThrow(Process target, TimeSpan? timeout)
{
if (!TryWaitForExit(target, timeout))
{
throw new TimeoutException("The process did not exit within the specified timeout.");
}
}
static bool TryWaitForExit(Process target, TimeSpan? timeout)
{
if (timeout is { } value)
{
var ms = (long)value.TotalMilliseconds;
if (ms < 0 || ms > int.MaxValue)
{
target.WaitForExit();
return;
}
if (!target.WaitForExit((int)ms))
if (ms >= 0 && ms <= int.MaxValue)
{
throw new TimeoutException("The process did not exit within the specified timeout.");
return target.WaitForExit((int)ms);
}
}
else
{
target.WaitForExit();
}
target.WaitForExit();
return true;
}
}
32 changes: 29 additions & 3 deletions src/Split/net461/ProcessPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ static partial class Polyfill
public static ProcessExitStatus Run(ProcessStartInfo startInfo, TimeSpan? timeout = default)
{
using var process = StartOrThrow(startInfo);
WaitForExitOrThrow(process, timeout);
if (!TryWaitForExit(process, timeout))
{
KillAndWait(process);
return new(-1, canceled: true);
}
return ToExitStatus(process.ExitCode);
}
/// <summary>
Expand All @@ -34,7 +38,14 @@ public static ProcessExitStatus Run(string fileName, IEnumerable<string>? argume
{
if (silent)
{
return Process.RunAndCaptureText(fileName, arguments, timeout).ExitStatus;
try
{
return Process.RunAndCaptureText(fileName, arguments, timeout).ExitStatus;
}
catch (TimeoutException)
{
return new(-1, canceled: true);
}
}
return Process.Run(BuildStartInfo(fileName, arguments), timeout);
}
Expand Down Expand Up @@ -97,7 +108,11 @@ public static ProcessTextOutput RunAndCaptureText(ProcessStartInfo startInfo, Ti
var stdoutTask = process.StandardOutput.ReadToEndAsync();
var stderrTask = process.StandardError.ReadToEndAsync();
var pid = process.Id;
WaitForExitOrThrow(process, timeout);
if (!TryWaitForExit(process, timeout))
{
KillAndWait(process);
throw new TimeoutException("The operation has timed out.");
}
var status = ToExitStatus(process.ExitCode);
return new(status, stdoutTask.GetAwaiter().GetResult(), stderrTask.GetAwaiter().GetResult(), pid);
}
Expand Down Expand Up @@ -235,6 +250,17 @@ static void ConfigureForCapture(ProcessStartInfo startInfo)
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
}
static void KillAndWait(Process process)
{
TryKill(process);
try
{
process.WaitForExit();
}
catch
{
}
}
static void TryKill(Process process)
{
try
Expand Down
22 changes: 11 additions & 11 deletions src/Split/net462/Polyfill_Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,23 +445,23 @@ static int ToSignalNumber(PosixSignal signal)
static extern int kill(int pid, int signal);
}
static void WaitForExitOrThrow(Process target, TimeSpan? timeout)
{
if (!TryWaitForExit(target, timeout))
{
throw new TimeoutException("The process did not exit within the specified timeout.");
}
}
static bool TryWaitForExit(Process target, TimeSpan? timeout)
{
if (timeout is { } value)
{
var ms = (long)value.TotalMilliseconds;
if (ms < 0 || ms > int.MaxValue)
{
target.WaitForExit();
return;
}
if (!target.WaitForExit((int)ms))
if (ms >= 0 && ms <= int.MaxValue)
{
throw new TimeoutException("The process did not exit within the specified timeout.");
return target.WaitForExit((int)ms);
}
}
else
{
target.WaitForExit();
}
target.WaitForExit();
return true;
}
}
32 changes: 29 additions & 3 deletions src/Split/net462/ProcessPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ static partial class Polyfill
public static ProcessExitStatus Run(ProcessStartInfo startInfo, TimeSpan? timeout = default)
{
using var process = StartOrThrow(startInfo);
WaitForExitOrThrow(process, timeout);
if (!TryWaitForExit(process, timeout))
{
KillAndWait(process);
return new(-1, canceled: true);
}
return ToExitStatus(process.ExitCode);
}
/// <summary>
Expand All @@ -34,7 +38,14 @@ public static ProcessExitStatus Run(string fileName, IEnumerable<string>? argume
{
if (silent)
{
return Process.RunAndCaptureText(fileName, arguments, timeout).ExitStatus;
try
{
return Process.RunAndCaptureText(fileName, arguments, timeout).ExitStatus;
}
catch (TimeoutException)
{
return new(-1, canceled: true);
}
}
return Process.Run(BuildStartInfo(fileName, arguments), timeout);
}
Expand Down Expand Up @@ -97,7 +108,11 @@ public static ProcessTextOutput RunAndCaptureText(ProcessStartInfo startInfo, Ti
var stdoutTask = process.StandardOutput.ReadToEndAsync();
var stderrTask = process.StandardError.ReadToEndAsync();
var pid = process.Id;
WaitForExitOrThrow(process, timeout);
if (!TryWaitForExit(process, timeout))
{
KillAndWait(process);
throw new TimeoutException("The operation has timed out.");
}
var status = ToExitStatus(process.ExitCode);
return new(status, stdoutTask.GetAwaiter().GetResult(), stderrTask.GetAwaiter().GetResult(), pid);
}
Expand Down Expand Up @@ -235,6 +250,17 @@ static void ConfigureForCapture(ProcessStartInfo startInfo)
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
}
static void KillAndWait(Process process)
{
TryKill(process);
try
{
process.WaitForExit();
}
catch
{
}
}
static void TryKill(Process process)
{
try
Expand Down
Loading
Loading