From b613d30334e1a1070fed1c84aea0a50df3acb809 Mon Sep 17 00:00:00 2001 From: Simon Krauter Date: Sun, 26 Oct 2014 01:13:35 +0200 Subject: [PATCH 1/2] Fix issue #1558 --- lib/pure/osproc.nim | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/pure/osproc.nim b/lib/pure/osproc.nim index 3c181bf53308..e66902b34225 100644 --- a/lib/pure/osproc.nim +++ b/lib/pure/osproc.nim @@ -49,6 +49,9 @@ type const poUseShell* {.deprecated.} = poUsePath ## Deprecated alias for poUsePath. + +const terminateDefaultMilliSecsToWait = 100 +const terminateMilliSecsPerLoop = 10 proc quoteShellWindows*(s: string): string {.noSideEffect, rtl, extern: "nosp$1".} = ## Quote s, so it can be safely passed to Windows API. @@ -163,7 +166,7 @@ proc suspend*(p: PProcess) {.rtl, extern: "nosp$1", tags: [].} proc resume*(p: PProcess) {.rtl, extern: "nosp$1", tags: [].} ## Resumes the process `p`. -proc terminate*(p: PProcess) {.rtl, extern: "nosp$1", tags: [].} +proc terminate*(p: PProcess, milliSecsToWait = terminateDefaultMilliSecsToWait) {.rtl, extern: "nosp$1", tags: [FTime].} ## Terminates the process `p`. proc running*(p: PProcess): bool {.rtl, extern: "nosp$1", tags: [].} @@ -471,7 +474,7 @@ when defined(Windows) and not defined(useNimRtl): var x = waitForSingleObject(p.fProcessHandle, 50) return x == WAIT_TIMEOUT - proc terminate(p: PProcess) = + proc terminate(p: PProcess, milliSecsToWait = terminateDefaultMilliSecsToWait) = if running(p): discard terminateProcess(p.fProcessHandle, 0) @@ -825,10 +828,17 @@ elif not defined(useNimRtl): if ret == 0: return true # Can't establish status. Assume running. result = ret == int(p.id) - proc terminate(p: PProcess) = - if kill(-p.id, SIGTERM) == 0'i32: + proc terminate(p: PProcess, milliSecsToWait = terminateDefaultMilliSecsToWait) = + var idToKill = p.id + if getpgid(idToKill) == idToKill: + idToKill = -idToKill + if kill(idToKill, SIGTERM) == 0'i32: + var timeToWait = milliSecsToWait + while p.running() and timeToWait > 0: + sleep(terminateMilliSecsPerLoop) + timeToWait = timeToWait - terminateMilliSecsPerLoop if p.running(): - if kill(-p.id, SIGKILL) != 0'i32: osError(osLastError()) + if kill(idToKill, SIGKILL) != 0'i32: osError(osLastError()) else: osError(osLastError()) proc waitForExit(p: PProcess, timeout: int = -1): int = From f35b564b8f40b5ba5d5e217dc1385d12f468316a Mon Sep 17 00:00:00 2001 From: Simon Krauter Date: Sun, 26 Oct 2014 01:54:48 +0200 Subject: [PATCH 2/2] Replaced -pid in suspend() and resume() too --- lib/pure/osproc.nim | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/pure/osproc.nim b/lib/pure/osproc.nim index e66902b34225..0f8afc40c18a 100644 --- a/lib/pure/osproc.nim +++ b/lib/pure/osproc.nim @@ -818,10 +818,16 @@ elif not defined(useNimRtl): discard close(p.errHandle) proc suspend(p: PProcess) = - if kill(-p.id, SIGSTOP) != 0'i32: osError(osLastError()) + var idToUse = p.id + if getpgid(idToUse) == idToUse: + idToUse = -idToUse + if kill(idToUse, SIGSTOP) != 0'i32: osError(osLastError()) proc resume(p: PProcess) = - if kill(-p.id, SIGCONT) != 0'i32: osError(osLastError()) + var idToUse = p.id + if getpgid(idToUse) == idToUse: + idToUse = -idToUse + if kill(idToUse, SIGCONT) != 0'i32: osError(osLastError()) proc running(p: PProcess): bool = var ret = waitpid(p.id, p.exitCode, WNOHANG) @@ -829,16 +835,16 @@ elif not defined(useNimRtl): result = ret == int(p.id) proc terminate(p: PProcess, milliSecsToWait = terminateDefaultMilliSecsToWait) = - var idToKill = p.id - if getpgid(idToKill) == idToKill: - idToKill = -idToKill - if kill(idToKill, SIGTERM) == 0'i32: + var idToUse = p.id + if getpgid(idToUse) == idToUse: + idToUse = -idToUse + if kill(idToUse, SIGTERM) == 0'i32: var timeToWait = milliSecsToWait while p.running() and timeToWait > 0: sleep(terminateMilliSecsPerLoop) timeToWait = timeToWait - terminateMilliSecsPerLoop if p.running(): - if kill(idToKill, SIGKILL) != 0'i32: osError(osLastError()) + if kill(idToUse, SIGKILL) != 0'i32: osError(osLastError()) else: osError(osLastError()) proc waitForExit(p: PProcess, timeout: int = -1): int =