fix async_wrapper start suspended race (#4718)

Main thread in started-suspended process may not be immediately resumable on slow targets- poll for proper state for awhile before attempting resume
This commit is contained in:
Matt Davis 2016-09-06 16:51:17 -07:00 committed by Matt Clay
parent 6099e6d19e
commit 009ebf619b

View file

@ -48,6 +48,7 @@ Function Start-Watchdog {
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
namespace Ansible.Async {
@ -89,7 +90,17 @@ Function Start-Watchdog {
{
var proc = Process.GetProcessById(pid);
foreach(var thread in proc.Threads.OfType<ProcessThread>().Where(t => t.WaitReason == ThreadWaitReason.Suspended))
// wait for at least one suspended thread in the process (this handles possible slow startup race where primary thread of created-suspended process has not yet become runnable)
var retryCount = 0;
while(!proc.Threads.OfType<ProcessThread>().Any(t=>t.ThreadState == System.Diagnostics.ThreadState.Wait && t.WaitReason == ThreadWaitReason.Suspended))
{
proc.Refresh();
Thread.Sleep(50);
if (retryCount > 100)
throw new InvalidOperationException(String.Format("No threads were suspended in target PID {0} after 5s", pid));
}
foreach(var thread in proc.Threads.OfType<ProcessThread>().Where(t => t.ThreadState == System.Diagnostics.ThreadState.Wait && t.WaitReason == ThreadWaitReason.Suspended))
ResumeThreadById(thread.Id);
}
}