Skip to content

Defensively ensure $wait() (and thus $kill(grace=)) reliably collects exit status on Unix - #429

Open
MichaelChirico wants to merge 4 commits into
r-lib:mainfrom
MichaelChirico:kill-collect-after-wait
Open

MichaelChirico wants to merge 4 commits into
r-lib:mainfrom
MichaelChirico:kill-collect-after-wait

Conversation

@MichaelChirico

Copy link
Copy Markdown
Contributor

On main, in rare situations, c_processx_wait can be both 0 and 1 incorrectly. This PR aims to better guarantee this routine's return value can be relied upon.

Strictly speaking, these edge cases are probably unusual enough that this PR could be rejected, but I'm sharing it as part of the series with #426, #427, #428, and one more forthcoming.


🤖 synopsis:

  • 0 when it should be 1:
    • Timeout vs. SIGCHLD race: poll() times out (ret == 0) and SIGCHLD arrives immediately afterward (before the signal mask is restored), reaping the child and setting handle->collected = 1, yet c_processx_wait() still returns 0. In c_processx_kill(), this falls through to kill(-pid, SIGKILL) on an already-reaped PID (PID-reuse risk) and returns FALSE even though its own SIGTERM killed the child.
    • Missed SIGCHLD with an unreaped zombie: If SIGCHLD is not delivered (e.g., under Valgrind or after another package overwrote the SIGCHLD handler) and the child exits as a zombie, kill(pid, 0) still returns 0 on a zombie, so poll() times out and returns 0 even though the child is already dead.
  • 1 when it should be 0 (or without collecting handle):
    • kill(pid, 0) != 0 on EPERM: The fallback treats any non-zero kill(pid, 0) as "exited", so EPERM (e.g., a child that execs a setuid binary) returns 1 while the child is still running.
    • kill(pid, 0) != 0 on ESRCH: Even when the child really did exit (e.g., reaped by another SIGCHLD handler), c_processx_wait() returns 1 without calling waitpid() / processx__collect_exit_status(), leaving handle->collected == 0, handle->exitcode == 0 (so c_processx_kill() checks 0 == -SIGTERM), and handle->pty_child_fd unclosed.

By passing SEXP status into c_processx_wait(), running a non-blocking waitpid() + processx__collect_exit_status() at cleanup: (with SIGCHLD blocked) whenever !handle->collected, and returning handle->collected, c_processx_wait() now returns 1 if and only if the child has exited and its status has been collected.

If `c_processx_wait()` returns while `handle->collected` is still 0 (for example via its `kill(pid, 0)` fallback when `SIGCHLD` was not delivered, or if `SIGCHLD` raced with `poll()` timing out), run a non-blocking `waitpid()` and call `processx__collect_exit_status()` before checking `handle->collected` or escalating to `SIGKILL`.
…lue is trustworthy

Pass `SEXP status` into `c_processx_wait()` and, at `cleanup:` with `SIGCHLD` blocked, run a non-blocking `waitpid()` + `processx__collect_exit_status()` if `!handle->collected`, returning `handle->collected`. This guarantees `c_processx_wait()` returns `1` iff the child has exited and its status has been collected, fixing both `()` and `c_processx_kill()` without needing duplicate reaping logic in `c_processx_kill()`.
@gaborcsardi

Copy link
Copy Markdown
Member
  • Timeout vs. SIGCHLD race: poll() times out (ret == 0) and SIGCHLD arrives immediately afterward (before the signal mask is restored), reaping the child and setting handle->collected = 1, yet c_processx_wait() still returns 0. In c_processx_kill(), this falls through to kill(-pid, SIGKILL) on an already-reaped PID (PID-reuse risk) and returns FALSE even though its own SIGTERM killed the child.

I don't think this is interesting or even incorrect. After c_processx_wait you cannot assume that the process is still running, anyway. So a 0 return just means that the process was running earlier, which is true.

  • Missed SIGCHLD with an unreaped zombie: If SIGCHLD is not delivered (e.g., under Valgrind or after another package overwrote the SIGCHLD handler) and the child exits as a zombie, kill(pid, 0) still returns 0 on a zombie, so poll() times out and returns 0 even though the child is already dead.

This can happen, well, maybe because we reinstall the signal handler at the beginning of each wait, AFAIR. But I don't think the PR fully fixes it, without a timeout the loop still spins forever, no?

  • kill(pid, 0) != 0 on EPERM: The fallback treats any non-zero kill(pid, 0) as "exited", so EPERM (e.g., a child that execs a setuid binary) returns 1 while the child is still running.

How can this happen if we started the child process?

  • kill(pid, 0) != 0 on ESRCH: Even when the child really did exit (e.g., reaped by another SIGCHLD handler), c_processx_wait() returns 1 without calling waitpid() / processx__collect_exit_status(), leaving handle->collected == 0, handle->exitcode == 0 (so c_processx_kill() checks 0 == -SIGTERM), and handle->pty_child_fd unclosed.

I don't think we can improve this, we miss the exit status, and the current code already sets it to NA already, AFAICT.

… and test zombie `wait()`

- Extract `processx__wait_try_collect()` (called with `SIGCHLD` blocked) and use it before `poll()`, inside the polling loop (replacing `kill(pid, 0)`), and at `cleanup:`.
- Fixes `p$wait()` hanging indefinitely on unreaped zombies (e.g. after `parallel::mcparallel()` overwrote `SIGCHLD`, or under Valgrind) and avoids `EPERM` false exits on setuid children.
- Add a `wait` test in `tests/testthat/test-sigchld.R` that hangs (times out) on `main` and passes with this fix.
…ollect()`, and tighten tests

- Initialize `wstat = 0` (`pid_t wp`) and simplify branching in `processx__wait_try_collect()`.
- Unblock `SIGCHLD` on `c_processx_wait()` error paths (preventing `SIGCHLD` from staying blocked when called from `c_processx_kill()`), route `pipe()` and `poll()` errors through `cleanup:`, drop redundant `processx__block_sigchld()`, and short-circuit `timeout == 0`.
- Rewrite `test_that("wait", ...)` in `tests/testthat/test-sigchld.R` with `callr::r()` + `px cat <stdin>` to avoid timing races, and drop the redundant test in `tests/testthat/test-process.R`.
@MichaelChirico

Copy link
Copy Markdown
Contributor Author

Yea, the PR wasn't quite right on your second point; updated to handle that more correctly by replacing kill(pid,0) with a waitpid(WNOHANG) helper blocked on SIGCHLD.

🤖 addressing the 1st, 3rd, and 4th comments:

  • Timeout vs. SIGCHLD race: Fair that the window is tiny, though it differs in one way from a process exiting after wait(): once c_processx_wait() returns, SIGCHLD is blocked again in c_processx_kill(), so a child exiting after wait() stays an unreaped zombie and its PID cannot be reused. In the race, SIGCHLD has already reaped pid (handle->collected == 1), so pid is eligible for PID/PGID reuse before c_processx_kill() falls through to kill(-pid, SIGKILL) — and c_processx_kill() returns FALSE even though handle->exitcode was already collected as -SIGTERM. Returning handle->collected avoids both for free.
  • EPERM: Can happen if a setuid child changes its real UID too (e.g. sudo or su calling setuid(0) / setresuid(0, 0, 0) so the unprivileged caller cannot signal it). waitpid() checks the parent-child relationship rather than UIDs (and cannot fail with EPERM), so switching to waitpid() avoids an early return after 200 ms automatically.
  • ESRCH: On main, c_processx_wait() doesn't call processx__collect_exit_status() on kill(pid, 0) != 0, so right after wait() returns, handle-collected is still 0 — meaning get_end_time() still returns NULL, handle->pty_child_fd stays open, and signal() won't short-circuit on handle-collected until a later waitpid() (e.g. via is_alive()/get_exit_status()/SIGCHLD) collects it as NA. Handling ECHILD in processx__wait_try_collect() leaves the handle collected immediately on return.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants