mirror of https://github.com/winsw/winsw
Fix termination
parent
db842409f1
commit
f02e63486b
|
@ -86,8 +86,6 @@ namespace WinSW.Util
|
|||
KeyValuePair<bool, bool> result = SignalHelper.SendCtrlCToProcess(process, stopTimeout);
|
||||
bool exited = result.Value;
|
||||
if (!exited)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool sent = result.Key;
|
||||
if (sent)
|
||||
|
@ -95,17 +93,12 @@ namespace WinSW.Util
|
|||
Logger.Warn("Process " + process.Id + " did not respond to Ctrl+C signal - Killing as fallback");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
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,
|
||||
string? workingDirectory = null,
|
||||
ProcessPriorityClass? priority = null,
|
||||
ProcessCompletionCallback? callback = null,
|
||||
Action<Process>? callback = null,
|
||||
bool redirectStdin = true,
|
||||
LogHandler? logHandler = null,
|
||||
bool hideWindow = false)
|
||||
|
@ -193,11 +186,11 @@ namespace WinSW.Util
|
|||
// monitor the completion of the process
|
||||
if (callback != null)
|
||||
{
|
||||
processToStart.Exited += (_, _) =>
|
||||
processToStart.Exited += (sender, _) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
callback(processToStart);
|
||||
callback((Process)sender!);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@ -209,6 +202,4 @@ namespace WinSW.Util
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public delegate void ProcessCompletionCallback(Process process);
|
||||
}
|
||||
|
|
|
@ -300,18 +300,11 @@ namespace WinSW
|
|||
this.process.EnableRaisingEvents = false;
|
||||
|
||||
if (stopArguments is null)
|
||||
{
|
||||
try
|
||||
{
|
||||
Log.Debug("ProcessKill " + this.process.Id);
|
||||
ProcessHelper.StopProcessTree(this.process, this.descriptor.StopTimeout);
|
||||
this.ExtensionManager.FireOnProcessTerminated(this.process);
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
// already terminated
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.SignalShutdownPending();
|
||||
|
@ -358,8 +351,6 @@ namespace WinSW
|
|||
effectiveProcessWaitSleepTime = (int)this.descriptor.SleepTime.TotalMilliseconds;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// WriteEvent("WaitForProcessToExit [start]");
|
||||
|
||||
while (!processoWait.WaitForExit(effectiveProcessWaitSleepTime))
|
||||
|
@ -367,11 +358,6 @@ namespace WinSW
|
|||
this.SignalShutdownPending();
|
||||
// WriteEvent("WaitForProcessToExit [repeat]");
|
||||
}
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
// already terminated
|
||||
}
|
||||
|
||||
// WriteEvent("WaitForProcessToExit [finished]");
|
||||
}
|
||||
|
@ -405,37 +391,27 @@ namespace WinSW
|
|||
private void StartProcess(Process processToStart, string arguments, string executable, LogHandler? logHandler, bool redirectStdin)
|
||||
{
|
||||
// Define handler of the completed process
|
||||
void OnProcessCompleted(Process proc)
|
||||
{
|
||||
string msg = processToStart.Id + " - " + processToStart.StartInfo.FileName + " " + processToStart.StartInfo.Arguments;
|
||||
try
|
||||
void OnProcessCompleted(Process process)
|
||||
{
|
||||
string msg = process.Id + " - " + process.StartInfo.FileName + " " + process.StartInfo.Arguments;
|
||||
|
||||
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
|
||||
{
|
||||
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.
|
||||
// by not reporting unclean shutdown, we let Windows SCM to decide if it wants to
|
||||
// restart the service automatically
|
||||
if (proc.ExitCode == 0)
|
||||
if (process.ExitCode == 0)
|
||||
{
|
||||
this.SignalShutdownComplete();
|
||||
}
|
||||
|
||||
Environment.Exit(proc.ExitCode);
|
||||
}
|
||||
}
|
||||
catch (InvalidOperationException ioe)
|
||||
{
|
||||
this.LogEvent("WaitForExit " + ioe.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
proc.Dispose();
|
||||
Environment.Exit(process.ExitCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue