1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-29 03:19:54 +00:00

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.
This commit is contained in:
Calvin Rose 2021-11-18 19:03:08 -06:00
parent 6660c1da38
commit ef7129f45d

View File

@ -415,17 +415,8 @@ static JanetEVGenericMessage janet_proc_wait_subr(JanetEVGenericMessage args) {
pid_t result; pid_t result;
int status = 0; int status = 0;
do { do {
result = waitpid(proc->pid, &status, 0); result = waitpid(proc->pid, &status, WNOWAIT);
} while (result == -1 && errno == EINTR); } 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; return args;
} }
@ -434,9 +425,22 @@ static JanetEVGenericMessage janet_proc_wait_subr(JanetEVGenericMessage args) {
/* Callback that is called in main thread when subroutine completes. */ /* Callback that is called in main thread when subroutine completes. */
static void janet_proc_wait_cb(JanetEVGenericMessage args) { static void janet_proc_wait_cb(JanetEVGenericMessage args) {
janet_ev_dec_refcount(); janet_ev_dec_refcount();
int status = args.argi;
JanetProc *proc = (JanetProc *) args.argp; JanetProc *proc = (JanetProc *) args.argp;
if (NULL != proc) { 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->return_code = (int32_t) status;
proc->flags |= JANET_PROC_WAITED; proc->flags |= JANET_PROC_WAITED;
proc->flags &= ~JANET_PROC_WAITING; proc->flags &= ~JANET_PROC_WAITING;