diff --git a/src/unix/processx-unix.h b/src/unix/processx-unix.h index 504b4c1a..c9f543ed 100644 --- a/src/unix/processx-unix.h +++ b/src/unix/processx-unix.h @@ -43,7 +43,7 @@ void processx__block_sigchld_save(sigset_t *old); void processx__unblock_sigchld(void); void processx__procmask_set(sigset_t *set); -int c_processx_wait(processx_handle_t *handle, int timeout, const char *name); +int c_processx_wait(SEXP status, int timeout, const char *name); int c_processx_kill(SEXP status, double grace, SEXP name); void processx__finalizer(SEXP status); diff --git a/src/unix/processx.c b/src/unix/processx.c index a75480ae..a9959f6d 100644 --- a/src/unix/processx.c +++ b/src/unix/processx.c @@ -734,18 +734,37 @@ SEXP processx_pty_close(SEXP status, SEXP name) { } SEXP processx_wait(SEXP status, SEXP timeout, SEXP name) { - processx_handle_t *handle = R_ExternalPtrAddr(status); int ctimeout = INTEGER(timeout)[0]; const char *cname = isNull(name) ? "???" : CHAR(STRING_ELT(name, 0)); - int ret = c_processx_wait(handle, ctimeout, cname); + int ret = c_processx_wait(status, ctimeout, cname); return ScalarLogical(ret); } -int c_processx_wait(processx_handle_t *handle, int timeout, const char *name) { +/* Must be called with SIGCHLD blocked. Checks whether the child has + exited (either already collected by SIGCHLD or via non-blocking + waitpid) and collects its exit status if so. Returns 1 if collected, + 0 if still running, or -1 on waitpid error (with errno preserved). */ +static int processx__wait_try_collect(SEXP status, processx_handle_t *handle) { + if (handle->collected) return 1; + + int wstat = 0; + pid_t wp; + do { + wp = waitpid(handle->pid, &wstat, WNOHANG); + } while (wp == -1 && errno == EINTR); + + if (wp == 0) return 0; + if (wp == -1 && errno != ECHILD) return -1; + + processx__collect_exit_status(status, wp, wstat); + return 1; +} + +int c_processx_wait(SEXP status, int timeout, const char *name) { + processx_handle_t *handle = R_ExternalPtrAddr(status); struct pollfd fd; - int ret = 0; - pid_t pid; + int ret = 0, err = 0; int timeleft = timeout; int *fds = malloc(sizeof(int) * 2); @@ -761,8 +780,6 @@ int c_processx_wait(processx_handle_t *handle, int timeout, const char *name) { return 1; } - pid = handle->pid; - /* If we already have the status, then return now. */ if (handle->collected) { processx__procmask_set(&old); @@ -771,12 +788,23 @@ int c_processx_wait(processx_handle_t *handle, int timeout, const char *name) { /* Make sure this is active, in case another package replaced it... */ processx__setup_sigchld(); - processx__block_sigchld(); + + /* The child may already have exited while SIGCHLD was not handled by us + (e.g., an unreaped zombie after another package overwrote SIGCHLD). */ + ret = processx__wait_try_collect(status, handle); + if (ret != 0) { + if (ret == -1) err = errno; + goto cleanup; + } + if (timeout == 0) { + processx__procmask_set(&old); + return 0; + } /* Setup the self-pipe that we can poll */ if (pipe(handle->waitpipe)) { - processx__procmask_set(&old); - R_THROW_SYSTEM_ERROR("processx error when waiting for '%s'", name); + err = errno; + goto cleanup; } fds[0] = handle->waitpipe[0]; fds[1] = handle->waitpipe[1]; @@ -802,15 +830,17 @@ int c_processx_wait(processx_handle_t *handle, int timeout, const char *name) { R_CheckUserInterrupt(); - /* We also check if the process is alive, because the SIGCHLD is - not delivered in valgrind :( This also works around the issue - of SIGCHLD handler interference, i.e. if another package (like - parallel) removes our signal handler. */ - ret = kill(pid, 0); + /* We also check if the process has exited, because SIGCHLD is not + delivered in valgrind :( This also works around SIGCHLD handler + interference, e.g. if another package (like parallel) removes our + signal handler. */ + processx__block_sigchld(); + ret = processx__wait_try_collect(status, handle); if (ret != 0) { - ret = 1; + if (ret == -1) err = errno; goto cleanup; } + processx__unblock_sigchld(); if (timeout >= 0) timeleft -= PROCESSX_INTERRUPT_INTERVAL; } @@ -823,18 +853,33 @@ int c_processx_wait(processx_handle_t *handle, int timeout, const char *name) { } if (ret == -1) { - R_THROW_SYSTEM_ERROR("processx wait with timeout error while " - "waiting for '%s'", name); + err = errno; + goto cleanup; } cleanup: + processx__block_sigchld(); /* pipe is closed in the on_exit handler */ handle->waitpipe[0] = -1; handle->waitpipe[1] = -1; - processx__procmask_set(&old); + int done = 0; + if (!err) { + done = processx__wait_try_collect(status, handle); + if (done == -1) err = errno; + } - return ret != 0; + if (err) { + processx__unblock_sigchld(); + R_THROW_SYSTEM_ERROR_CODE( + err, + "processx wait error while waiting for '%s'", + name + ); + } + + processx__procmask_set(&old); + return done; } /* This is similar to `processx_wait`, but a bit simpler, because we @@ -1091,7 +1136,7 @@ int c_processx_kill(SEXP status, double grace, SEXP name) { if (grace_ms) { KILL_WITH(SIGTERM); - if (c_processx_wait(handle, grace_ms, cname)) { + if (c_processx_wait(status, grace_ms, cname)) { result = handle->exitcode == -SIGTERM; goto cleanup; } diff --git a/tests/testthat/test-sigchld.R b/tests/testthat/test-sigchld.R index 088e64cd..eb8b7dfc 100644 --- a/tests/testthat/test-sigchld.R +++ b/tests/testthat/test-sigchld.R @@ -148,6 +148,36 @@ test_that("kill", { expect_true(res$result$status %in% c(0L, NA_integer_)) }) +test_that("wait", { + skip_other_platforms("unix") + skip_on_cran() + + res <- callr::r( + function(px_tool) { + library(parallel) + library(processx) + + px <- process$new(px_tool, c("cat", ""), stdin = "|", stdout = "|") + on.exit(try(px$kill(), silent = TRUE), add = TRUE) + + mccollect(mcparallel(NULL)) + Sys.sleep(0.05) + + close(px$get_input_connection()) + px$poll_io(5000) + Sys.sleep(0.05) + + px$wait() + list(alive = px$is_alive(), status = px$get_exit_status()) + }, + args = list(px_tool = get_tool("px")), + timeout = if (is_san()) 60 else 10 + ) + + expect_false(res$alive) + expect_true(res$status %in% c(0L, NA_integer_)) +}) + test_that("SIGCHLD handler", { skip_other_platforms("unix") skip_on_cran()