ps icon indicating copy to clipboard operation
ps copied to clipboard

Add `ps_kill_parallel()`

Open lionel- opened this issue 1 year ago • 1 comments

Kills a list of process handles in parallel with a grace period.

  • First a SIGTERM is issued to allow processes to gracefully terminate if they can.

  • We listen for termination events of immediate subprocesses via a SIGCHLD handler that wakes up a bottom half with a one-byte write to a self-pipe. This mostly follows what processx is doing for the wait() method. The self-pipe polling is reguarly interrupted to check for user interrupts and we take this opportunity to check for termination of subsubprocesses for which we don't get notified on termination.

    This means that if the list only contains immediate subprocesses we are able to return quicker than if it contains indirect subprocesses.

  • When the grace period is up and some processes are still running, those are sent SIGKILL.

  • The creation time of the process is consistently checked to avoid killing reused PIDs.

Next steps:

  • Use in ps_kill_tree() to add graceful termination to processx kill_tree() methods. I suggest replacing the sig argument by grace, and add a separate ps_signal_tree() for sending custom signals without the grace mechanism. A CRAN org search shows that the sig argument is not used on CRAN.

  • Use in processx session finalizers to sigterm all processes in parallel on session quit.

lionel- avatar Apr 26 '23 16:04 lionel-

The windows tests are failing because on Windows we loop over processes with ps_kill(). The create-time test creates a process handle with a bumped create-time to simulate a PID reuse and on Windows we get:

Error: No such process, pid 1044, ???

Is this expected behaviour? It seems a bit dangerous to throw an error just because a process might have been killed or terminated already. Considering the races involved and that ps_kill() will mostly be called in cleanup context, it seems that this makes it hard to use.

lionel- avatar Apr 27 '23 07:04 lionel-