From ef7129f45d1239e10d63e03eec93a2776ac3fea1 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Thu, 18 Nov 2021 19:03:08 -0600 Subject: [PATCH] Address #874 - Call waitpid on waiter thread with WNOWAIT. This doesn't destory the pid until the original thread decides to call waitpid again. Since the pid is exposed in the C API and now in the Janet API, we don't want to destroy it until we are ready. --- src/core/os.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/core/os.c b/src/core/os.c index 8c75eac0..752e1dd1 100644 --- a/src/core/os.c +++ b/src/core/os.c @@ -415,17 +415,8 @@ static JanetEVGenericMessage janet_proc_wait_subr(JanetEVGenericMessage args) { pid_t result; int status = 0; do { - result = waitpid(proc->pid, &status, 0); + result = waitpid(proc->pid, &status, WNOWAIT); } while (result == -1 && errno == EINTR); - /* Use POSIX shell semantics for interpreting signals */ - if (WIFEXITED(status)) { - status = WEXITSTATUS(status); - } else if (WIFSTOPPED(status)) { - status = WSTOPSIG(status) + 128; - } else { - status = WTERMSIG(status) + 128; - } - args.argi = status; return args; } @@ -434,9 +425,22 @@ static JanetEVGenericMessage janet_proc_wait_subr(JanetEVGenericMessage args) { /* Callback that is called in main thread when subroutine completes. */ static void janet_proc_wait_cb(JanetEVGenericMessage args) { janet_ev_dec_refcount(); - int status = args.argi; JanetProc *proc = (JanetProc *) args.argp; if (NULL != proc) { + /* Wait again without NOWAIT. + * Use POSIX shell semantics for interpreting signals */ + int status = 0; + pid_t result; + do { + result = waitpid(proc->pid, &status, 0); + } while (result == -1 && errno == EINTR); + if (WIFEXITED(status)) { + status = WEXITSTATUS(status); + } else if (WIFSTOPPED(status)) { + status = WSTOPSIG(status) + 128; + } else { + status = WTERMSIG(status) + 128; + } proc->return_code = (int32_t) status; proc->flags |= JANET_PROC_WAITED; proc->flags &= ~JANET_PROC_WAITING;