Issue #59 - Prevent failure when Child processes cannot be retrieved due to the system shutdown

pull/172/head
Oleg Nenashev 2016-12-24 21:40:57 +01:00
parent ab51b50d85
commit 10bcbde081
1 changed files with 15 additions and 6 deletions

View File

@ -90,23 +90,32 @@ namespace winsw.Util
/// <param name="stopParentProcessFirst">If enabled, the perent process will be terminated before its children on all levels</param>
public static void StopProcessAndChildren(int pid, TimeSpan stopTimeout, bool stopParentProcessFirst)
{
var childPids = GetChildPids(pid);
if (stopParentProcessFirst)
List<int> childPids = null;
try
{
StopProcess(pid, stopTimeout);
childPids = GetChildPids(pid);
}
catch (Exception ex)
{
Logger.Warn("Failed to locate children of the process with PID=" + pid + ". Child processes won't be terminated", ex);
}
if (!stopParentProcessFirst && childPids != null)
{
foreach (var childPid in childPids)
{
StopProcessAndChildren(childPid, stopTimeout, stopParentProcessFirst);
}
}
else
StopProcess(pid, stopTimeout);
if (stopParentProcessFirst && childPids != null)
{
foreach (var childPid in childPids)
{
StopProcessAndChildren(childPid, stopTimeout, stopParentProcessFirst);
}
StopProcess(pid, stopTimeout);
}
}