Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,19 @@ public static int Main(string[] args)

using IDisposable processLifecycle = ProcessLifecycle.Initialize();
CancellationToken cancellationToken = ProcessLifecycle.CancellationToken;
using CancellationTokenRegistration registration = childMode is "uncooperative" or "repeated"
using IDisposable softCancellationTimeout =
childMode is "repeated" or "extended-grace"
? ProcessLifecycle.OverrideSoftCancellationTimeout(Timeout.InfiniteTimeSpan)
: null!;
using CancellationTokenRegistration registration =
childMode is "cooperative" or "uncooperative" or "repeated" or "extended-grace"
? cancellationToken.Register(() => WriteMarker(markerPath, "signal-cancelled"))
: default;

WriteMarker(markerPath, "ready");

if (childMode == "repeated")
{
using IDisposable softCancellationTimeout =
ProcessLifecycle.OverrideSoftCancellationTimeout(Timeout.InfiniteTimeSpan);
Thread.Sleep(Timeout.Infinite);
}

Expand All @@ -83,17 +86,16 @@ public static int Main(string[] args)

if (childMode == "forceful-cooperative")
{
using CancellationTokenRegistration forcefulRegistration =
ProcessLifecycle.ForcefulCancellationToken.Register(
() => WriteMarker(markerPath, "forceful-cancelled"));
ProcessLifecycle.ForcefulCancellationToken.WaitHandle.WaitOne();
WriteMarker(markerPath, "forceful-cancelled");
return GetSignalExitCode(cancellationToken);
}

if (childMode == "extended-grace")
{
cancellationToken.WaitHandle.WaitOne();
using IDisposable softCancellationTimeout =
ProcessLifecycle.OverrideSoftCancellationTimeout(Timeout.InfiniteTimeSpan);
WriteMarker(markerPath, "signal-cancelled");
Thread.Sleep(TimeSpan.FromMilliseconds(750));
WriteMarker(markerPath, "override-completed");
return GetSignalExitCode(cancellationToken);
Expand All @@ -111,6 +113,8 @@ private static void WriteMarker(string path, string value)
using FileStream stream = new(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
using StreamWriter writer = new(stream);
writer.Write(value);
writer.Flush();
File.WriteAllText(path + "." + value, "");
}

private static void RunReaperParent(string[] args, string markerPath, string childMode)
Expand Down
41 changes: 32 additions & 9 deletions test/Microsoft.DotNet.Cli.Utils.Tests/ProcessLifecycleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests;
public class ProcessLifecycleTests : SdkTest
{
private static readonly TimeSpan s_waitTimeout = TimeSpan.FromSeconds(30);
private static readonly TimeSpan s_processTimingTimeout = TimeSpan.FromSeconds(10);

[TestMethod]
[OSCondition(ConditionMode.Exclude, OperatingSystems.Windows)]
Expand Down Expand Up @@ -68,7 +69,7 @@ public void SigIntForcesTerminationAfterSoftCancellationTimeout()
child.WaitForExit(s_waitTimeout).Should().BeTrue();

child.ExitCode.Should().Be(130);
stopwatch.Elapsed.Should().BeLessThan(TimeSpan.FromSeconds(1.5));
stopwatch.Elapsed.Should().BeLessThan(s_processTimingTimeout);
}

[TestMethod]
Expand Down Expand Up @@ -114,8 +115,8 @@ public void SecondSigIntBypassesSoftCancellationTimeoutOverride()
child.WaitForExit(s_waitTimeout).Should().BeTrue();

child.ExitCode.Should().Be(130);
stopwatch.Elapsed.Should().BeLessThan(TimeSpan.FromSeconds(1));
File.ReadAllText(GetMarkerPath(testDirectory.Path)).Should().Be("signal-cancelled");
stopwatch.Elapsed.Should().BeLessThan(s_processTimingTimeout);
MarkerHasValue(GetMarkerPath(testDirectory.Path), "signal-cancelled").Should().BeTrue();
}

[TestMethod]
Expand Down Expand Up @@ -209,7 +210,7 @@ public void ManagedCliWaitsForSecondSigIntBeforeTerminatingExternalCommand()
child.WaitForExit(s_waitTimeout).Should().BeTrue();

child.ExitCode.Should().Be(130);
stopwatch.Elapsed.Should().BeLessThan(TimeSpan.FromSeconds(1));
stopwatch.Elapsed.Should().BeLessThan(s_processTimingTimeout);
string standardError = stderrTask.GetAwaiter().GetResult();
standardError.Should().NotContain("Unhandled exception");
standardError.Should().NotContain("The operation was canceled");
Expand Down Expand Up @@ -250,7 +251,7 @@ public void SigTermIsForwardedToReapedChild()
uncooperativeReaperParent.SafeHandle.Signal(PosixSignal.SIGTERM).Should().BeTrue();
uncooperativeReaperParent.WaitForExit(s_waitTimeout).Should().BeTrue();
uncooperativeReaperParent.ExitCode.Should().NotBe(0);
stopwatch.Elapsed.Should().BeLessThan(TimeSpan.FromSeconds(2));
stopwatch.Elapsed.Should().BeLessThan(s_processTimingTimeout);
}

[TestMethod]
Expand Down Expand Up @@ -319,7 +320,7 @@ private void VerifyRepeatedSignal(PosixSignal signal, int expectedExitCode)
SendSignal(child, testDirectory.Path, signal, sequence: 2);
child.WaitForExit(s_waitTimeout).Should().BeTrue();
child.ExitCode.Should().Be(expectedExitCode);
stopwatch.Elapsed.Should().BeLessThan(TimeSpan.FromSeconds(1));
stopwatch.Elapsed.Should().BeLessThan(s_processTimingTimeout);
}
finally
{
Expand All @@ -331,8 +332,13 @@ private static Process StartChild(string childMode, string testDirectory)
{
string markerPath = GetMarkerPath(testDirectory);
File.Delete(markerPath);
File.Delete(markerPath + ".signal-1");
File.Delete(markerPath + ".signal-2");
foreach (string signalMarkerPath in Directory.EnumerateFiles(
testDirectory,
Path.GetFileName(markerPath) + ".*"))
{
File.Delete(signalMarkerPath);
}

Process process = CreateChildProcess(childMode, testDirectory);
process.Start();
return process;
Expand Down Expand Up @@ -395,11 +401,28 @@ private static void WaitForMarker(string testDirectory, string expectedValue)
private static bool TryWaitForMarker(string testDirectory, string expectedValue)
{
string markerPath = GetMarkerPath(testDirectory);
return SpinWait.SpinUntil(() => MarkerHasValue(markerPath, expectedValue), s_waitTimeout);
Stopwatch stopwatch = Stopwatch.StartNew();
do
{
if (MarkerHasValue(markerPath, expectedValue))
{
return true;
}

Thread.Sleep(TimeSpan.FromMilliseconds(10));
}
while (stopwatch.Elapsed < s_waitTimeout);

return MarkerHasValue(markerPath, expectedValue);
}

private static bool MarkerHasValue(string markerPath, string expectedValue)
{
if (File.Exists(markerPath + "." + expectedValue))
{
return true;
}

try
{
if (!File.Exists(markerPath))
Expand Down
Loading