Skip to content

Commit

Permalink
Fix 5-minute hang in Process tests (#44571)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Nov 12, 2020
1 parent c6947e0 commit 3b5a94c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ public void ModulesAreDisposedWhenProcessIsDisposed()
processModule.Disposed += (_, __) => disposedCount += 1;
}

process.Dispose();
KillWait(process);
Assert.Equal(0, disposedCount);

process.Dispose();
Assert.Equal(expectedCount, disposedCount);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;
Expand All @@ -11,14 +12,14 @@ namespace System.Diagnostics.Tests
{
partial class ProcessTestBase
{
protected Process CreateProcessLong()
protected Process CreateProcessLong([CallerMemberName] string callerName = null)
{
return CreateSleepProcess(RemotelyInvokable.WaitInMS);
return CreateSleepProcess(RemotelyInvokable.WaitInMS, callerName);
}

protected Process CreateSleepProcess(int durationMs)
protected Process CreateSleepProcess(int durationMs, [CallerMemberName] string callerName = null)
{
return CreateProcess(RemotelyInvokable.Sleep, durationMs.ToString());
return CreateProcess(RemotelyInvokable.Sleep, durationMs.ToString(), callerName);
}

protected Process CreateProcessPortable(Func<int> func)
Expand Down
35 changes: 29 additions & 6 deletions src/libraries/System.Diagnostics.Process/tests/ProcessTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.RemoteExecutor;
Expand All @@ -17,12 +17,10 @@ public partial class ProcessTestBase : FileCleanupTestBase
protected Process _process;
protected readonly List<Process> _processes = new List<Process>();

protected Process CreateDefaultProcess()
protected Process CreateDefaultProcess([CallerMemberName] string callerName = null)
{
if (_process != null)
throw new InvalidOperationException();

_process = CreateProcessLong();
Assert.Null(_process);
_process = CreateProcessLong(callerName);
_process.Start();
AddProcessForDispose(_process);
return _process;
Expand Down Expand Up @@ -85,8 +83,28 @@ protected Process CreateProcess(Func<string, int> method, string arg, bool autoD
p = handle.Process;
handle.Process = null;
}

if (autoDispose)
{
AddProcessForDispose(p);
}

return p;
}

protected Process CreateProcess(Func<string, string, int> method, string arg1, string arg2, bool autoDispose = true)
{
Process p = null;
using (RemoteInvokeHandle handle = RemoteExecutor.Invoke(method, arg1, arg2, new RemoteInvokeOptions { Start = false }))
{
p = handle.Process;
handle.Process = null;
}

if (autoDispose)
{
AddProcessForDispose(p);
}

return p;
}
Expand All @@ -107,6 +125,11 @@ protected void StartSleepKillWait(Process p)
{
p.Start();
Thread.Sleep(200);
KillWait(p);
}

protected void KillWait(Process p)
{
p.Kill();
Assert.True(p.WaitForExit(WaitInMS));
p.WaitForExit(); // wait for event handlers to complete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ public void ThreadsAreDisposedWhenProcessIsDisposed()
processThread.Disposed += (_, __) => disposedCount += 1;
}

process.Dispose();
KillWait(process);
Assert.Equal(0, disposedCount);

process.Dispose();
Assert.Equal(expectedCount, disposedCount);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ public static int Dummy()
return SuccessExitCode;
}

public static int Sleep(string duration)
public static int Sleep(string duration, string callerName)
{
_ = callerName; // argument ignored, for debugging purposes
Thread.Sleep(int.Parse(duration));
return SuccessExitCode;
}
Expand Down

0 comments on commit 3b5a94c

Please sign in to comment.