From dfafc2790d80f58c74ac17d36682c4c02ba9a4f2 Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Sat, 26 Nov 2016 23:16:33 +0100 Subject: [PATCH 1/2] [Issue #124] - Prevent the CPU overutilization due to the usage of Milliseconds instead of TotalMsec --- src/Core/ServiceWrapper/Main.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Core/ServiceWrapper/Main.cs b/src/Core/ServiceWrapper/Main.cs index dd18771..3e8fead 100644 --- a/src/Core/ServiceWrapper/Main.cs +++ b/src/Core/ServiceWrapper/Main.cs @@ -440,12 +440,24 @@ namespace winsw private void WaitForProcessToExit(Process processoWait) { SignalShutdownPending(); + + int effectiveProcessWaitSleepTime; + if (_descriptor.SleepTime.TotalMilliseconds > Int32.MaxValue) + { + Log.Warn("The requested sleep time " + _descriptor.SleepTime.TotalMilliseconds + "is greater that the max value " + + Int32.MaxValue + ". The value will be truncated"); + effectiveProcessWaitSleepTime = Int32.MaxValue; + } + else + { + effectiveProcessWaitSleepTime = (int)_descriptor.SleepTime.TotalMilliseconds; + } try { // WriteEvent("WaitForProcessToExit [start]"); - while (!processoWait.WaitForExit(_descriptor.SleepTime.Milliseconds)) + while (!processoWait.WaitForExit(effectiveProcessWaitSleepTime)) { SignalShutdownPending(); // WriteEvent("WaitForProcessToExit [repeat]"); From 2a07f6af5da3f2364139be48acac536fd85e41fa Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Sat, 26 Nov 2016 23:18:03 +0100 Subject: [PATCH 2/2] Address another Milliseconds misusage (similar to #124) --- src/Core/ServiceWrapper/Main.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Core/ServiceWrapper/Main.cs b/src/Core/ServiceWrapper/Main.cs index 3e8fead..4fc66c8 100644 --- a/src/Core/ServiceWrapper/Main.cs +++ b/src/Core/ServiceWrapper/Main.cs @@ -473,9 +473,22 @@ namespace winsw private void SignalShutdownPending() { + int effectiveWaitHint; + if (_descriptor.WaitHint.TotalMilliseconds > Int32.MaxValue) + { + Log.Warn("The requested WaitHint value (" + _descriptor.WaitHint.TotalMilliseconds + " ms) is greater that the max value " + + Int32.MaxValue + ". The value will be truncated"); + effectiveWaitHint = Int32.MaxValue; + } + else + { + effectiveWaitHint = (int)_descriptor.WaitHint.TotalMilliseconds; + } + + IntPtr handle = ServiceHandle; _wrapperServiceStatus.checkPoint++; - _wrapperServiceStatus.waitHint = _descriptor.WaitHint.Milliseconds; + _wrapperServiceStatus.waitHint = effectiveWaitHint; // WriteEvent("SignalShutdownPending " + wrapperServiceStatus.checkPoint + ":" + wrapperServiceStatus.waitHint); _wrapperServiceStatus.currentState = (int)State.SERVICE_STOP_PENDING; Advapi32.SetServiceStatus(handle, ref _wrapperServiceStatus);