Address - 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.
@@ -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;
