From 52c3f7f74e5b9c9ae20b4b854ad78f47b267d412 Mon Sep 17 00:00:00 2001 From: chiricom Date: Sun, 29 Nov 2026 21:01:55 +0000 Subject: [PATCH 1/3] Fix `waitpipe` fd cleanup and `SIGCHLD` mask restoration on exit/unwind - In `processx__wait_cleanup()` and `c_processx_wait()`, block `SIGCHLD` while closing only open `handle->waitpipe[0..1]` fds and resetting them to `-1`, preventing `processx__sigchld_callback()` from closing a reused fd after an interrupted `wait()` and preventing double-closes of `waitpipe[1]`. - Register `cleancall` exit handlers in both `c_processx_wait()` and `c_processx_kill()` to restore the saved `SIGCHLD` signal mask on any error or interrupt unwind. --- src/unix/processx.c | 117 ++++++++++++++++++++++++++++--------- tests/testthat/test-wait.R | 25 ++++++++ 2 files changed, 113 insertions(+), 29 deletions(-) diff --git a/src/unix/processx.c b/src/unix/processx.c index a75480ae..4e3728f2 100644 --- a/src/unix/processx.c +++ b/src/unix/processx.c @@ -695,12 +695,41 @@ void processx__collect_exit_status(SEXP status, int retval, int wstat) { } } +typedef struct { + processx_handle_t *handle; + sigset_t old_mask; + int restore_mask; +} processx__wait_cleanup_t; + static void processx__wait_cleanup(void *ptr) { - int *fds = (int*) ptr; - if (!fds) return; - if (fds[0] >= 0) close(fds[0]); - if (fds[1] >= 0) close(fds[1]); - free(fds); + processx__wait_cleanup_t *data = (processx__wait_cleanup_t *) ptr; + if (!data) return; + + /* Block SIGCHLD while closing and resetting waitpipe so + processx__sigchld_callback() cannot race on waitpipe[1]. */ + sigset_t block, prev; + sigemptyset(&block); + sigaddset(&block, SIGCHLD); + sigprocmask(SIG_BLOCK, &block, &prev); + + if (data->handle) { + if (data->handle->waitpipe[0] >= 0) { + close(data->handle->waitpipe[0]); + data->handle->waitpipe[0] = -1; + } + if (data->handle->waitpipe[1] >= 0) { + close(data->handle->waitpipe[1]); + data->handle->waitpipe[1] = -1; + } + } + + if (data->restore_mask) { + sigprocmask(SIG_SETMASK, &data->old_mask, NULL); + } else { + sigprocmask(SIG_SETMASK, &prev, NULL); + } + + free(data); } /* In general we need to worry about three asynchronous processes here: @@ -748,25 +777,27 @@ int c_processx_wait(processx_handle_t *handle, int timeout, const char *name) { pid_t pid; int timeleft = timeout; - int *fds = malloc(sizeof(int) * 2); - if (!fds) R_THROW_SYSTEM_ERROR("Allocating memory when waiting"); - fds[0] = fds[1] = -1; - r_call_on_exit(processx__wait_cleanup, fds); + processx__wait_cleanup_t *cleanup_data = + malloc(sizeof(processx__wait_cleanup_t)); + if (!cleanup_data) R_THROW_SYSTEM_ERROR("Allocating memory when waiting"); + cleanup_data->handle = handle; + cleanup_data->restore_mask = 0; + r_call_on_exit(processx__wait_cleanup, cleanup_data); - sigset_t old; - processx__block_sigchld_save(&old); + processx__block_sigchld_save(&cleanup_data->old_mask); + cleanup_data->restore_mask = 1; if (!handle) { - processx__procmask_set(&old); - return 1; + ret = 1; + goto cleanup; } pid = handle->pid; /* If we already have the status, then return now. */ if (handle->collected) { - processx__procmask_set(&old); - return 1; + ret = 1; + goto cleanup; } /* Make sure this is active, in case another package replaced it... */ @@ -775,11 +806,8 @@ int c_processx_wait(processx_handle_t *handle, int timeout, const char *name) { /* 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); } - fds[0] = handle->waitpipe[0]; - fds[1] = handle->waitpipe[1]; processx__nonblock_fcntl(handle->waitpipe[0], 1); processx__nonblock_fcntl(handle->waitpipe[1], 1); @@ -790,8 +818,6 @@ int c_processx_wait(processx_handle_t *handle, int timeout, const char *name) { processx__unblock_sigchld(); - - while (timeout < 0 || timeleft > PROCESSX_INTERRUPT_INTERVAL) { do { ret = poll(&fd, 1, PROCESSX_INTERRUPT_INTERVAL); @@ -828,11 +854,22 @@ int c_processx_wait(processx_handle_t *handle, int timeout, const char *name) { } cleanup: - /* pipe is closed in the on_exit handler */ - handle->waitpipe[0] = -1; - handle->waitpipe[1] = -1; - - processx__procmask_set(&old); + /* Block SIGCHLD while closing and resetting waitpipe so + processx__sigchld_callback() cannot race on waitpipe[1]. */ + processx__block_sigchld(); + if (handle) { + if (handle->waitpipe[0] >= 0) { + close(handle->waitpipe[0]); + handle->waitpipe[0] = -1; + } + if (handle->waitpipe[1] >= 0) { + close(handle->waitpipe[1]); + handle->waitpipe[1] = -1; + } + } + cleanup_data->handle = NULL; + cleanup_data->restore_mask = 0; + processx__procmask_set(&cleanup_data->old_mask); return ret != 0; } @@ -1042,13 +1079,36 @@ SEXP processx_kill(SEXP status, SEXP grace, SEXP name) { return ScalarLogical(c_processx_kill(status, REAL(grace)[0], name)); } +typedef struct { + sigset_t old_mask; + int restore_mask; +} processx__sigmask_cleanup_t; + +static void processx__sigmask_cleanup(void *ptr) { + processx__sigmask_cleanup_t *data = (processx__sigmask_cleanup_t *) ptr; + if (!data) return; + if (data->restore_mask) { + sigprocmask(SIG_SETMASK, &data->old_mask, NULL); + } + free(data); +} + int c_processx_kill(SEXP status, double grace, SEXP name) { processx_handle_t *handle = R_ExternalPtrAddr(status); const char *cname = isNull(name) ? "???" : CHAR(STRING_ELT(name, 0)); pid_t pid; int wstat, wp, result = 0; - processx__block_sigchld(); + processx__sigmask_cleanup_t *sigmask_cleanup = + malloc(sizeof(processx__sigmask_cleanup_t)); + if (!sigmask_cleanup) { + R_THROW_SYSTEM_ERROR("Allocating memory when killing '%s'", cname); + } + sigmask_cleanup->restore_mask = 0; + r_call_on_exit(processx__sigmask_cleanup, sigmask_cleanup); + + processx__block_sigchld_save(&sigmask_cleanup->old_mask); + sigmask_cleanup->restore_mask = 1; if (!handle) { goto cleanup; } @@ -1070,7 +1130,6 @@ int c_processx_kill(SEXP status, double grace, SEXP name) { /* Some other error? */ if (wp == -1) { - processx__unblock_sigchld(); R_THROW_SYSTEM_ERROR("processx_kill for '%s'", cname); } @@ -1084,7 +1143,6 @@ int c_processx_kill(SEXP status, double grace, SEXP name) { int ret = kill(-pid, SIG); \ if (ret == -1 && (errno == ESRCH || errno == EPERM)) { goto cleanup; } \ if (ret == -1) { \ - processx__unblock_sigchld(); \ R_THROW_SYSTEM_ERROR("process_kill for '%s'", cname); \ } \ } while(0) @@ -1114,7 +1172,8 @@ int c_processx_kill(SEXP status, double grace, SEXP name) { result = handle->exitcode == - SIGKILL; cleanup: - processx__unblock_sigchld(); + sigmask_cleanup->restore_mask = 0; + processx__procmask_set(&sigmask_cleanup->old_mask); return result; } diff --git a/tests/testthat/test-wait.R b/tests/testthat/test-wait.R index 487a4b36..4884c310 100644 --- a/tests/testthat/test-wait.R +++ b/tests/testthat/test-wait.R @@ -75,3 +75,28 @@ test_that("no fd leak on unix", { expect_equal(res$result$fd1, res$result$fd2) expect_s3_class(res$result$err, "interrupt") }) + +test_that("interrupted wait() does not close newly opened file descriptors", { + skip_on_os("windows") + skip_on_cran() + + px <- get_tool("px") + p <- process$new(px, c("sleep", "5"), poll_connection = FALSE) + withr::defer(p$kill()) + + err <- interrupt_me(p$wait(), after = 0.3) + expect_s3_class(err, "interrupt") + + f1 <- withr::local_tempfile() + f2 <- withr::local_tempfile() + c1 <- conn_create_file(f1, read = FALSE, write = TRUE) + c2 <- conn_create_file(f2, read = FALSE, write = TRUE) + withr::defer(close(c1)) + withr::defer(close(c2)) + + tools::pskill(p$get_pid(), tools::SIGKILL) + retry_until(function() !p$is_alive()) + + expect_equal(conn_write(c1, "ok"), raw()) + expect_equal(conn_write(c2, "ok"), raw()) +}) From 217a61e092244e82f7948f7f68fb8aff748ab03a Mon Sep 17 00:00:00 2001 From: chiricom Date: Mon, 30 Nov 2026 03:48:10 +0000 Subject: [PATCH 2/3] Deduplicate `waitpipe` cleanup into `processx__wait_finish()` and add interrupted `kill(grace = ...)` test - Extract `processx__wait_finish()` guarded by a single `active` flag so normal return and unwind share the same cleanup path with no redundant `sigprocmask()` syscalls on normal exit. - Add a test in `tests/testthat/test-wait.R` verifying that interrupting `p$kill(grace = ...)` neither closes newly opened file descriptors nor leaves `SIGCHLD` blocked. --- src/unix/processx.c | 44 ++++++++++++------------------------- tests/testthat/test-wait.R | 45 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 30 deletions(-) diff --git a/src/unix/processx.c b/src/unix/processx.c index 4e3728f2..6b9e01b6 100644 --- a/src/unix/processx.c +++ b/src/unix/processx.c @@ -698,19 +698,19 @@ void processx__collect_exit_status(SEXP status, int retval, int wstat) { typedef struct { processx_handle_t *handle; sigset_t old_mask; - int restore_mask; + int active; } processx__wait_cleanup_t; -static void processx__wait_cleanup(void *ptr) { - processx__wait_cleanup_t *data = (processx__wait_cleanup_t *) ptr; - if (!data) return; +static void processx__wait_finish(processx__wait_cleanup_t *data) { + if (!data || !data->active) return; + data->active = 0; /* Block SIGCHLD while closing and resetting waitpipe so processx__sigchld_callback() cannot race on waitpipe[1]. */ - sigset_t block, prev; + sigset_t block; sigemptyset(&block); sigaddset(&block, SIGCHLD); - sigprocmask(SIG_BLOCK, &block, &prev); + sigprocmask(SIG_BLOCK, &block, NULL); if (data->handle) { if (data->handle->waitpipe[0] >= 0) { @@ -723,12 +723,12 @@ static void processx__wait_cleanup(void *ptr) { } } - if (data->restore_mask) { - sigprocmask(SIG_SETMASK, &data->old_mask, NULL); - } else { - sigprocmask(SIG_SETMASK, &prev, NULL); - } + sigprocmask(SIG_SETMASK, &data->old_mask, NULL); +} +static void processx__wait_cleanup(void *ptr) { + processx__wait_cleanup_t *data = (processx__wait_cleanup_t *) ptr; + processx__wait_finish(data); free(data); } @@ -781,11 +781,11 @@ int c_processx_wait(processx_handle_t *handle, int timeout, const char *name) { malloc(sizeof(processx__wait_cleanup_t)); if (!cleanup_data) R_THROW_SYSTEM_ERROR("Allocating memory when waiting"); cleanup_data->handle = handle; - cleanup_data->restore_mask = 0; + cleanup_data->active = 0; r_call_on_exit(processx__wait_cleanup, cleanup_data); processx__block_sigchld_save(&cleanup_data->old_mask); - cleanup_data->restore_mask = 1; + cleanup_data->active = 1; if (!handle) { ret = 1; @@ -854,23 +854,7 @@ int c_processx_wait(processx_handle_t *handle, int timeout, const char *name) { } cleanup: - /* Block SIGCHLD while closing and resetting waitpipe so - processx__sigchld_callback() cannot race on waitpipe[1]. */ - processx__block_sigchld(); - if (handle) { - if (handle->waitpipe[0] >= 0) { - close(handle->waitpipe[0]); - handle->waitpipe[0] = -1; - } - if (handle->waitpipe[1] >= 0) { - close(handle->waitpipe[1]); - handle->waitpipe[1] = -1; - } - } - cleanup_data->handle = NULL; - cleanup_data->restore_mask = 0; - processx__procmask_set(&cleanup_data->old_mask); - + processx__wait_finish(cleanup_data); return ret != 0; } diff --git a/tests/testthat/test-wait.R b/tests/testthat/test-wait.R index 4884c310..694cca05 100644 --- a/tests/testthat/test-wait.R +++ b/tests/testthat/test-wait.R @@ -100,3 +100,48 @@ test_that("interrupted wait() does not close newly opened file descriptors", { expect_equal(conn_write(c1, "ok"), raw()) expect_equal(conn_write(c2, "ok"), raw()) }) + +test_that("interrupted kill() with grace cleans up fds and signal handling", { + skip_on_os("windows") + skip_on_cran() + skip_if_not_installed("callr") + + interrupter <- callr::r_session$new() + withr::defer(interrupter$close()) + + px <- get_tool("px") + p <- process$new( + px, + c("sigterm", "ignore", "outln", "ready", "sleep", "10"), + stdout = "|", + poll_connection = FALSE + ) + pid <- p$get_pid() + withr::defer(p$kill()) + + p$poll_io(2000) + expect_equal(p$read_output_lines(), "ready") + + interrupter$call( + function(pid) { + Sys.sleep(0.2) + tools::pskill(pid, tools::SIGINT) + }, + list(pid = Sys.getpid()) + ) + err <- tryCatch(p$kill(grace = 5), interrupt = function(e) e) + expect_s3_class(err, "interrupt") + + f1 <- withr::local_tempfile() + f2 <- withr::local_tempfile() + c1 <- conn_create_file(f1, read = FALSE, write = TRUE) + c2 <- conn_create_file(f2, read = FALSE, write = TRUE) + withr::defer(close(c1)) + withr::defer(close(c2)) + + tools::pskill(pid, tools::SIGKILL) + retry_until(function() !process__exists(pid)) + + expect_equal(conn_write(c1, "ok"), raw()) + expect_equal(conn_write(c2, "ok"), raw()) +}) From 76ec4067a7f559e2a092cf34ea8f47bfd9ec7584 Mon Sep 17 00:00:00 2001 From: chiricom Date: Mon, 30 Nov 2026 22:14:28 +0000 Subject: [PATCH 3/3] Drop `processx__sigmask_cleanup_t` and harden `test-wait.R` tests - In `c_processx_kill()`, save `sigset_t old` on the stack, restore `&old` before `c_processx_wait()`, re-block `SIGCHLD` afterward, and check `exited || handle->collected`, eliminating `processx__sigmask_cleanup_t` (no `malloc`/R allocation on every `kill()`, and no GC-finalizer reentrancy hazard into `r_call_on_exit()`). - In `c_processx_wait()`, return early on `!handle` before allocating `cleanup_data`, drop redundant `processx__block_sigchld()`, gate `SIG_BLOCK` + `close()` in `processx__wait_finish()` on `waitpipe[0] >= 0`, and note why raw `sigprocmask()` is used inside exit handlers. - Isolate both new tests in `tests/testthat/test-wait.R` inside `callr::r_session` subprocesses synchronized on a ready file, with `skip_if_not_installed("callr")`, `skip_if(is_valgrind())`, `skip_if(is_san())`, and `!process__exists(pid)` verification. --- src/unix/processx.c | 76 ++++++---------- tests/testthat/test-wait.R | 176 ++++++++++++++++++++++++++----------- 2 files changed, 155 insertions(+), 97 deletions(-) diff --git a/src/unix/processx.c b/src/unix/processx.c index 6b9e01b6..ba5ee0b2 100644 --- a/src/unix/processx.c +++ b/src/unix/processx.c @@ -705,18 +705,18 @@ static void processx__wait_finish(processx__wait_cleanup_t *data) { if (!data || !data->active) return; data->active = 0; - /* Block SIGCHLD while closing and resetting waitpipe so - processx__sigchld_callback() cannot race on waitpipe[1]. */ - sigset_t block; - sigemptyset(&block); - sigaddset(&block, SIGCHLD); - sigprocmask(SIG_BLOCK, &block, NULL); - - if (data->handle) { - if (data->handle->waitpipe[0] >= 0) { - close(data->handle->waitpipe[0]); - data->handle->waitpipe[0] = -1; - } + /* Called from cleancall exit handlers too, so use sigprocmask() directly + rather than throwing helpers (exit handlers must not jump). */ + if (data->handle->waitpipe[0] >= 0) { + /* Block SIGCHLD while closing and resetting waitpipe so + processx__sigchld_callback() cannot race on waitpipe[1]. */ + sigset_t block; + sigemptyset(&block); + sigaddset(&block, SIGCHLD); + sigprocmask(SIG_BLOCK, &block, NULL); + + close(data->handle->waitpipe[0]); + data->handle->waitpipe[0] = -1; if (data->handle->waitpipe[1] >= 0) { close(data->handle->waitpipe[1]); data->handle->waitpipe[1] = -1; @@ -777,6 +777,10 @@ int c_processx_wait(processx_handle_t *handle, int timeout, const char *name) { pid_t pid; int timeleft = timeout; + if (!handle) { + return 1; + } + processx__wait_cleanup_t *cleanup_data = malloc(sizeof(processx__wait_cleanup_t)); if (!cleanup_data) R_THROW_SYSTEM_ERROR("Allocating memory when waiting"); @@ -787,11 +791,6 @@ int c_processx_wait(processx_handle_t *handle, int timeout, const char *name) { processx__block_sigchld_save(&cleanup_data->old_mask); cleanup_data->active = 1; - if (!handle) { - ret = 1; - goto cleanup; - } - pid = handle->pid; /* If we already have the status, then return now. */ @@ -802,7 +801,6 @@ 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(); /* Setup the self-pipe that we can poll */ if (pipe(handle->waitpipe)) { @@ -1063,36 +1061,14 @@ SEXP processx_kill(SEXP status, SEXP grace, SEXP name) { return ScalarLogical(c_processx_kill(status, REAL(grace)[0], name)); } -typedef struct { - sigset_t old_mask; - int restore_mask; -} processx__sigmask_cleanup_t; - -static void processx__sigmask_cleanup(void *ptr) { - processx__sigmask_cleanup_t *data = (processx__sigmask_cleanup_t *) ptr; - if (!data) return; - if (data->restore_mask) { - sigprocmask(SIG_SETMASK, &data->old_mask, NULL); - } - free(data); -} - int c_processx_kill(SEXP status, double grace, SEXP name) { processx_handle_t *handle = R_ExternalPtrAddr(status); const char *cname = isNull(name) ? "???" : CHAR(STRING_ELT(name, 0)); pid_t pid; int wstat, wp, result = 0; - processx__sigmask_cleanup_t *sigmask_cleanup = - malloc(sizeof(processx__sigmask_cleanup_t)); - if (!sigmask_cleanup) { - R_THROW_SYSTEM_ERROR("Allocating memory when killing '%s'", cname); - } - sigmask_cleanup->restore_mask = 0; - r_call_on_exit(processx__sigmask_cleanup, sigmask_cleanup); - - processx__block_sigchld_save(&sigmask_cleanup->old_mask); - sigmask_cleanup->restore_mask = 1; + sigset_t old; + processx__block_sigchld_save(&old); if (!handle) { goto cleanup; } @@ -1114,7 +1090,9 @@ int c_processx_kill(SEXP status, double grace, SEXP name) { /* Some other error? */ if (wp == -1) { - R_THROW_SYSTEM_ERROR("processx_kill for '%s'", cname); + int err = errno; + processx__procmask_set(&old); + R_THROW_SYSTEM_ERROR_CODE(err, "processx_kill for '%s'", cname); } /* If the process is not running, return (FALSE) */ @@ -1127,13 +1105,18 @@ int c_processx_kill(SEXP status, double grace, SEXP name) { int ret = kill(-pid, SIG); \ if (ret == -1 && (errno == ESRCH || errno == EPERM)) { goto cleanup; } \ if (ret == -1) { \ - R_THROW_SYSTEM_ERROR("process_kill for '%s'", cname); \ + int err = errno; \ + processx__procmask_set(&old); \ + R_THROW_SYSTEM_ERROR_CODE(err, "process_kill for '%s'", cname); \ } \ } while(0) if (grace_ms) { KILL_WITH(SIGTERM); - if (c_processx_wait(handle, grace_ms, cname)) { + processx__procmask_set(&old); + int exited = c_processx_wait(handle, grace_ms, cname); + processx__block_sigchld(); + if (exited || handle->collected) { result = handle->exitcode == -SIGTERM; goto cleanup; } @@ -1156,8 +1139,7 @@ int c_processx_kill(SEXP status, double grace, SEXP name) { result = handle->exitcode == - SIGKILL; cleanup: - sigmask_cleanup->restore_mask = 0; - processx__procmask_set(&sigmask_cleanup->old_mask); + processx__procmask_set(&old); return result; } diff --git a/tests/testthat/test-wait.R b/tests/testthat/test-wait.R index 694cca05..c9ccc023 100644 --- a/tests/testthat/test-wait.R +++ b/tests/testthat/test-wait.R @@ -79,69 +79,145 @@ test_that("no fd leak on unix", { test_that("interrupted wait() does not close newly opened file descriptors", { skip_on_os("windows") skip_on_cran() + skip_if_not_installed("callr") + skip_if(is_valgrind()) + skip_if(is_san()) - px <- get_tool("px") - p <- process$new(px, c("sleep", "5"), poll_connection = FALSE) - withr::defer(p$kill()) - - err <- interrupt_me(p$wait(), after = 0.3) - expect_s3_class(err, "interrupt") + rs <- callr::r_session$new() + withr::defer(rs$close()) + + ready <- withr::local_tempfile() + rs$call( + function(px_tool, ready) { + p <- processx::process$new( + px_tool, + c("sleep", "60"), + poll_connection = FALSE + ) + on.exit(try(p$kill(), silent = TRUE), add = TRUE) + pid <- p$get_pid() + + f1 <- tempfile() + f2 <- tempfile() + on.exit(unlink(c(f1, f2)), add = TRUE) + gc() + + file.create(ready) + err <- tryCatch(p$wait(), interrupt = function(e) e) + + c1 <- processx::conn_create_file(f1, read = FALSE, write = TRUE) + c2 <- processx::conn_create_file(f2, read = FALSE, write = TRUE) + on.exit(close(c1), add = TRUE) + on.exit(close(c2), add = TRUE) + + tools::pskill(pid, tools::SIGKILL) + # process__exists() uses kill(pid, 0) without reaping zombies, so it + # becomes FALSE only after SIGCHLD itself has reaped the child. + deadline <- Sys.time() + 5 + while (processx:::process__exists(pid) && Sys.time() < deadline) { + Sys.sleep(0.05) + } + + list( + err = err, + reaped = !processx:::process__exists(pid), + w1 = processx::conn_write(c1, "ok"), + w2 = processx::conn_write(c2, "ok") + ) + }, + list(px_tool = get_tool("px"), ready = ready) + ) - f1 <- withr::local_tempfile() - f2 <- withr::local_tempfile() - c1 <- conn_create_file(f1, read = FALSE, write = TRUE) - c2 <- conn_create_file(f2, read = FALSE, write = TRUE) - withr::defer(close(c1)) - withr::defer(close(c2)) + retry_until(function() file.exists(ready), interrupt = 0.02, timeout = 10) + Sys.sleep(0.1) + rs$interrupt() - tools::pskill(p$get_pid(), tools::SIGKILL) - retry_until(function() !p$is_alive()) + deadline <- Sys.time() + 10 + res <- NULL + while (is.null(res) && Sys.time() < deadline) { + if (rs$poll_process(500) == "ready") { + msg <- rs$read() + if (!is.null(msg) && msg$code != 301L) { + res <- msg + } + } + } - expect_equal(conn_write(c1, "ok"), raw()) - expect_equal(conn_write(c2, "ok"), raw()) + expect_false(is.null(res)) + expect_null(res$error) + expect_s3_class(res$result$err, "interrupt") + expect_true(res$result$reaped) + expect_equal(res$result$w1, raw()) + expect_equal(res$result$w2, raw()) }) -test_that("interrupted kill() with grace cleans up fds and signal handling", { +test_that("interrupted kill() with grace restores signal handling", { skip_on_os("windows") skip_on_cran() skip_if_not_installed("callr") + skip_if(is_valgrind()) + skip_if(is_san()) - interrupter <- callr::r_session$new() - withr::defer(interrupter$close()) - - px <- get_tool("px") - p <- process$new( - px, - c("sigterm", "ignore", "outln", "ready", "sleep", "10"), - stdout = "|", - poll_connection = FALSE - ) - pid <- p$get_pid() - withr::defer(p$kill()) - - p$poll_io(2000) - expect_equal(p$read_output_lines(), "ready") - - interrupter$call( - function(pid) { - Sys.sleep(0.2) - tools::pskill(pid, tools::SIGINT) + rs <- callr::r_session$new() + withr::defer(rs$close()) + + ready <- withr::local_tempfile() + rs$call( + function(px_tool, ready) { + p <- processx::process$new( + px_tool, + c("sigterm", "ignore", "outln", "ready", "sleep", "60"), + stdout = "|", + poll_connection = FALSE + ) + on.exit(try(p$kill(), silent = TRUE), add = TRUE) + pid <- p$get_pid() + + deadline <- Sys.time() + 5 + out <- character() + while (!("ready" %in% out) && Sys.time() < deadline) { + p$poll_io(500) + out <- c(out, p$read_output_lines()) + } + + file.create(ready) + err <- tryCatch(p$kill(grace = 60), interrupt = function(e) e) + + tools::pskill(pid, tools::SIGKILL) + # process__exists() uses kill(pid, 0) without reaping zombies, so it + # becomes FALSE only if SIGCHLD is unblocked and reaped the child. + deadline <- Sys.time() + 5 + while (processx:::process__exists(pid) && Sys.time() < deadline) { + Sys.sleep(0.05) + } + + list( + ready = "ready" %in% out, + err = err, + reaped = !processx:::process__exists(pid) + ) }, - list(pid = Sys.getpid()) + list(px_tool = get_tool("px"), ready = ready) ) - err <- tryCatch(p$kill(grace = 5), interrupt = function(e) e) - expect_s3_class(err, "interrupt") - f1 <- withr::local_tempfile() - f2 <- withr::local_tempfile() - c1 <- conn_create_file(f1, read = FALSE, write = TRUE) - c2 <- conn_create_file(f2, read = FALSE, write = TRUE) - withr::defer(close(c1)) - withr::defer(close(c2)) + retry_until(function() file.exists(ready), interrupt = 0.02, timeout = 10) + Sys.sleep(0.1) + rs$interrupt() - tools::pskill(pid, tools::SIGKILL) - retry_until(function() !process__exists(pid)) + deadline <- Sys.time() + 10 + res <- NULL + while (is.null(res) && Sys.time() < deadline) { + if (rs$poll_process(500) == "ready") { + msg <- rs$read() + if (!is.null(msg) && msg$code != 301L) { + res <- msg + } + } + } - expect_equal(conn_write(c1, "ok"), raw()) - expect_equal(conn_write(c2, "ok"), raw()) + expect_false(is.null(res)) + expect_null(res$error) + expect_true(res$result$ready) + expect_s3_class(res$result$err, "interrupt") + expect_true(res$result$reaped) })