Fix service crashes (#680)

pull/694/head
Next Turn 2020-09-03 21:44:20 +08:00 committed by GitHub
parent bff88217d3
commit 7c61b9c43c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 3 deletions

View File

@ -4,6 +4,7 @@ using System.Diagnostics;
using System.Management; using System.Management;
using System.Threading; using System.Threading;
using log4net; using log4net;
using WinSW.Native;
namespace WinSW.Util namespace WinSW.Util
{ {
@ -168,7 +169,19 @@ namespace WinSW.Util
} }
} }
bool succeeded = ConsoleApis.SetConsoleCtrlHandler(null, false); // inherited
Debug.Assert(succeeded);
try
{
processToStart.Start(); processToStart.Start();
}
finally
{
succeeded = ConsoleApis.SetConsoleCtrlHandler(null, true);
Debug.Assert(succeeded);
}
Logger.Info("Started process " + processToStart.Id); Logger.Info("Started process " + processToStart.Id);
if (priority != null) if (priority != null)

View File

@ -29,9 +29,10 @@ namespace WinSW.Util
return new KeyValuePair<bool, bool>(false, error == Errors.ERROR_INVALID_PARAMETER); return new KeyValuePair<bool, bool>(false, error == Errors.ERROR_INVALID_PARAMETER);
} }
_ = ConsoleApis.SetConsoleCtrlHandler(null, true); // Don't call GenerateConsoleCtrlEvent immediately after SetConsoleCtrlHandler.
// A delay was observed as of Windows 10, version 2004 and Windows Server 2019.
_ = ConsoleApis.GenerateConsoleCtrlEvent(ConsoleApis.CtrlEvents.CTRL_C_EVENT, 0); _ = ConsoleApis.GenerateConsoleCtrlEvent(ConsoleApis.CtrlEvents.CTRL_C_EVENT, 0);
_ = ConsoleApis.SetConsoleCtrlHandler(null, false);
bool succeeded = ConsoleApis.FreeConsole(); bool succeeded = ConsoleApis.FreeConsole();
Debug.Assert(succeeded); Debug.Assert(succeeded);

View File

@ -182,6 +182,9 @@ namespace WinSW
protected override void OnStart(string[] args) protected override void OnStart(string[] args)
{ {
bool succeeded = ConsoleApis.SetConsoleCtrlHandler(null, true);
Debug.Assert(succeeded);
this.envs = this.descriptor.EnvironmentVariables; this.envs = this.descriptor.EnvironmentVariables;
// TODO: Disabled according to security concerns in https://github.com/kohsuke/winsw/issues/54 // TODO: Disabled according to security concerns in https://github.com/kohsuke/winsw/issues/54