From 7d0cdfc79dfa9c2c631cc41e3ccca62e19d8041e Mon Sep 17 00:00:00 2001 From: Mark Leyva Date: Mon, 1 Jul 2024 02:42:11 -0700 Subject: [PATCH] fixes #5091; Ensure we don't wait on an exited process on Linux (#23743) Fixes #5091. Ensure we don't wait on an exited process on Linux (cherry picked from commit 288d5c4ac32c27704258a15d365a56dadf38b0e9) --- lib/pure/osproc.nim | 2 ++ tests/osproc/twaitforexit.nim | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 tests/osproc/twaitforexit.nim diff --git a/lib/pure/osproc.nim b/lib/pure/osproc.nim index e30f1da737..503d68d06a 100644 --- a/lib/pure/osproc.nim +++ b/lib/pure/osproc.nim @@ -1416,6 +1416,8 @@ elif not defined(useNimRtl): tmspec.tv_nsec = (timeout * 1_000_000) try: + if not running(p): + return exitStatusLikeShell(p.exitStatus) if clock_gettime(CLOCK_REALTIME, stspec) == -1: raiseOSError(osLastError()) while true: diff --git a/tests/osproc/twaitforexit.nim b/tests/osproc/twaitforexit.nim new file mode 100644 index 0000000000..5db8d25666 --- /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: + discard + + # check that we don't have to wait msWait milliseconds + doAssert(getTime() < atStart + milliseconds(msWait)) \ No newline at end of file