Fix termination

pull/595/head
NextTurn 2020-07-22 00:00:00 +08:00 committed by Next Turn
parent db842409f1
commit f02e63486b
2 changed files with 34 additions and 67 deletions

View File

@ -86,8 +86,6 @@ namespace WinSW.Util
KeyValuePair<bool, bool> result = SignalHelper.SendCtrlCToProcess(process, stopTimeout); KeyValuePair<bool, bool> result = SignalHelper.SendCtrlCToProcess(process, stopTimeout);
bool exited = result.Value; bool exited = result.Value;
if (!exited) if (!exited)
{
try
{ {
bool sent = result.Key; bool sent = result.Key;
if (sent) if (sent)
@ -95,17 +93,12 @@ namespace WinSW.Util
Logger.Warn("Process " + process.Id + " did not respond to Ctrl+C signal - Killing as fallback"); Logger.Warn("Process " + process.Id + " did not respond to Ctrl+C signal - Killing as fallback");
} }
try
{
process.Kill(); process.Kill();
} }
catch (Exception ex) catch when (process.HasExited)
{ {
if (!process.HasExited)
{
throw;
}
// Process already exited.
Logger.Warn("Ignoring exception from killing process because it has exited", ex);
} }
} }
@ -146,7 +139,7 @@ namespace WinSW.Util
Dictionary<string, string>? envVars = null, Dictionary<string, string>? envVars = null,
string? workingDirectory = null, string? workingDirectory = null,
ProcessPriorityClass? priority = null, ProcessPriorityClass? priority = null,
ProcessCompletionCallback? callback = null, Action<Process>? callback = null,
bool redirectStdin = true, bool redirectStdin = true,
LogHandler? logHandler = null, LogHandler? logHandler = null,
bool hideWindow = false) bool hideWindow = false)
@ -193,11 +186,11 @@ namespace WinSW.Util
// monitor the completion of the process // monitor the completion of the process
if (callback != null) if (callback != null)
{ {
processToStart.Exited += (_, _) => processToStart.Exited += (sender, _) =>
{ {
try try
{ {
callback(processToStart); callback((Process)sender!);
} }
catch (Exception e) catch (Exception e)
{ {
@ -209,6 +202,4 @@ namespace WinSW.Util
} }
} }
} }
public delegate void ProcessCompletionCallback(Process process);
} }

View File

@ -300,18 +300,11 @@ namespace WinSW
this.process.EnableRaisingEvents = false; this.process.EnableRaisingEvents = false;
if (stopArguments is null) if (stopArguments is null)
{
try
{ {
Log.Debug("ProcessKill " + this.process.Id); Log.Debug("ProcessKill " + this.process.Id);
ProcessHelper.StopProcessTree(this.process, this.descriptor.StopTimeout); ProcessHelper.StopProcessTree(this.process, this.descriptor.StopTimeout);
this.ExtensionManager.FireOnProcessTerminated(this.process); this.ExtensionManager.FireOnProcessTerminated(this.process);
} }
catch (InvalidOperationException)
{
// already terminated
}
}
else else
{ {
this.SignalShutdownPending(); this.SignalShutdownPending();
@ -358,8 +351,6 @@ namespace WinSW
effectiveProcessWaitSleepTime = (int)this.descriptor.SleepTime.TotalMilliseconds; effectiveProcessWaitSleepTime = (int)this.descriptor.SleepTime.TotalMilliseconds;
} }
try
{
// WriteEvent("WaitForProcessToExit [start]"); // WriteEvent("WaitForProcessToExit [start]");
while (!processoWait.WaitForExit(effectiveProcessWaitSleepTime)) while (!processoWait.WaitForExit(effectiveProcessWaitSleepTime))
@ -367,11 +358,6 @@ namespace WinSW
this.SignalShutdownPending(); this.SignalShutdownPending();
// WriteEvent("WaitForProcessToExit [repeat]"); // WriteEvent("WaitForProcessToExit [repeat]");
} }
}
catch (InvalidOperationException)
{
// already terminated
}
// WriteEvent("WaitForProcessToExit [finished]"); // WriteEvent("WaitForProcessToExit [finished]");
} }
@ -405,37 +391,27 @@ namespace WinSW
private void StartProcess(Process processToStart, string arguments, string executable, LogHandler? logHandler, bool redirectStdin) private void StartProcess(Process processToStart, string arguments, string executable, LogHandler? logHandler, bool redirectStdin)
{ {
// Define handler of the completed process // Define handler of the completed process
void OnProcessCompleted(Process proc) void OnProcessCompleted(Process process)
{
string msg = processToStart.Id + " - " + processToStart.StartInfo.FileName + " " + processToStart.StartInfo.Arguments;
try
{ {
string msg = process.Id + " - " + process.StartInfo.FileName + " " + process.StartInfo.Arguments;
if (this.orderlyShutdown) if (this.orderlyShutdown)
{ {
this.LogEvent("Child process [" + msg + "] terminated with " + proc.ExitCode, EventLogEntryType.Information); this.LogEvent("Child process [" + msg + "] terminated with " + process.ExitCode, EventLogEntryType.Information);
} }
else else
{ {
this.LogEvent("Child process [" + msg + "] finished with " + proc.ExitCode, EventLogEntryType.Warning); this.LogEvent("Child process [" + msg + "] finished with " + process.ExitCode, EventLogEntryType.Warning);
// if we finished orderly, report that to SCM. // if we finished orderly, report that to SCM.
// by not reporting unclean shutdown, we let Windows SCM to decide if it wants to // by not reporting unclean shutdown, we let Windows SCM to decide if it wants to
// restart the service automatically // restart the service automatically
if (proc.ExitCode == 0) if (process.ExitCode == 0)
{ {
this.SignalShutdownComplete(); this.SignalShutdownComplete();
} }
Environment.Exit(proc.ExitCode); Environment.Exit(process.ExitCode);
}
}
catch (InvalidOperationException ioe)
{
this.LogEvent("WaitForExit " + ioe.Message);
}
finally
{
proc.Dispose();
} }
} }