Repository navigation
Fix wait() cleanup to prevent closing incorrect descriptors after interrupt - #430
Open
MichaelChirico wants to merge 3 commits into
Open
MichaelChirico wants to merge 3 commits into
MichaelChirico wants to merge 3 commits into
Conversation
- 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.
… 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.
- 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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently, there are two sources of truth about whether the
waitpipeentries for a process have been cleared: thehandleitself (handle->waitpipe) and theint *fdscopy passed toprocessx__wait_cleanup.When
p$wait()(and thusp$kill(grace=)) is interrupted (SIGINT),cleanup:is skipped, soprocessx__wait_cleanup()runsclose()on thefds, buthandle->waitpipeis not reset to-1. Thus, a future object picking up the samefdIDs can be closed inadvertently when the child later exits.(there's also a double-
close()going on where bothprocessx__sigchld_callback()andprocessx__wait_cleanup()runclose()onhandle->waitpipe[1])Here, we make
handle->waitpipethe single source of truth, shared betweenprocessx__wait_cleanupandc_processx_wait'scleanup:clause viaprocessx__wait_finish(). In turn, this restoresc_processx_wait()'s saved signal mask, so inc_processx_kill()we restore the saved mask before callingc_processx_wait()and re-blockSIGCHLDafterward soSIGCHLDisn't left blocked on unwind.🤖 synopsis
On Unix,
c_processx_wait()currently tracks the self-pipe file descriptors in two places —handle->waitpipe[0..1](read by theSIGCHLDhandler) and a separateint *fdscopy passed tor_call_on_exit(processx__wait_cleanup, fds). This causes three bugs:p$wait()orp$kill(grace = ...)is interrupted bySIGINT(R_CheckUserInterrupt()),c_processx_wait()unwinds before reachingcleanup:.processx__wait_cleanup()closesfds[0..1], leaving the old fd numbers inhandle->waitpipe[0..1]. When the R session later opens a new file/connection/socket that reuseshandle->waitpipe[1]and the child eventually exits,processx__sigchld_callback()closes that newly opened fd (EBADF).waitpipe[1]: When a child exits normally duringwait(),processx__sigchld_callback()closeshandle->waitpipe[1]and sets it to-1, butprocessx__wait_cleanup()still closes its stale copyfds[1]on exit.SIGCHLDleft blocked on unwind:c_processx_kill()callsc_processx_wait()withSIGCHLDblocked and neither restores its saved signal mask across an unwind, so an error unwind insidec_processx_wait()(e.g.pipe()or allocation failure) can leaveSIGCHLDpermanently blocked.Changes
fdscopy inc_processx_wait()withprocessx__wait_cleanup_tholdinghandle,old_mask, and anactiveflag, sohandle->waitpipeis the single source of truth.processx__wait_finish()betweencleanup:andprocessx__wait_cleanup()to blockSIGCHLDwhenwaitpipeis open, close only open (>= 0) ends ofhandle->waitpipewhile resetting them to-1, and restoreold_maskon both normal return and unwind (with no redundantsigprocmask()syscalls on normal exit).c_processx_kill(), saveoldon the stack, restore it before callingc_processx_wait(), and re-blockSIGCHLDafterward (checkingexited || handle->collected), so unwinding out ofp$kill(grace = ...)leavesSIGCHLDunblocked with no extramalloc/exit-handler overhead.callr::r_sessiontests intests/testthat/test-wait.R: one verifying interruptedwait()does not close newly opened file descriptors (fails onmainwithBad file descriptor), and one verifying interruptedkill(grace = ...)leavesSIGCHLDunblocked so subsequent child exits are still reaped (process__exists()).