diff --git a/lib/pure/osproc.nim b/lib/pure/osproc.nim index 9284e823af74f..e6cb09e2616cf 100644 --- a/lib/pure/osproc.nim +++ b/lib/pure/osproc.nim @@ -1383,7 +1383,8 @@ elif not defined(useNimRtl): s.tv_sec = b.tv_sec s.tv_nsec = b.tv_nsec - if p.exitFlag: + # Ensure we don't wait on an exited process + if not(p.running()) and p.exitFlag: return exitStatusLikeShell(p.exitStatus) if timeout == -1: diff --git a/tests/osproc/twaitforexit.nim b/tests/osproc/twaitforexit.nim new file mode 100644 index 0000000000000..7d1dfeec795ca --- /dev/null +++ b/tests/osproc/twaitforexit.nim @@ -0,0 +1,18 @@ +import std/[osproc, os, times] + +block: # bug #5091 + when defined(linux): + const filename = "false" + var p = startProcess(filename, options = {poStdErrToStdOut, poUsePath}) + os.sleep(1000) # make sure process has exited already + + let atStart = getTime() + const msWait = 2000 + + try: + discard waitForExit(p, msWait) + except OSError: + echo getCurrentExceptionMsg() + + # check that we don't have to wait msWait milliseconds + doAssert(getTime() < atStart + milliseconds(msWait)) \ No newline at end of file