r-lib / ps

R package to query, list, manipulate system processes
https://ps.r-lib.org/
Other
77 stars 19 forks source link

Add `ps_kill_parallel()` #148

Closed lionel- closed 1 month ago

lionel- commented 1 year ago

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

Next steps:

lionel- commented 1 year ago

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.

gaborcsardi commented 1 month ago

Maybe we don't need to make it interruptible? After all the grace period will be typically small, and people can wait a couple of seconds.

But of course the main issue is the SIGCHLD. I wonder if we can do better for that nowadays. Probably not, but let me look around a bit. AFAIR there are some macOS specific and/or Linux specific APIs that will let you poll for the termination of multiple processes.

gaborcsardi commented 1 month ago

Seems like we can use pidfd_open() on Linux, and macOS has kqueue, which can poll for process termination.

gaborcsardi commented 1 month ago

pidfd_open() works great, it is a pity that it needs a Linux kernel 5.3 or later. RHEL 8.10 has 4.18, and it is supported until 2029 (!), so we'll clearly need a fallback, using the self-pipe and the SIGCHLD handler.

Debian 10 also has 4.x, but it is EOL June 2024, so that is OK.

gaborcsardi commented 1 month ago

OK, with the new ps_wait() this is going to be much simpler, so I am going to close this and create another PR.

I am sorry again for the long wait, and thank you for the PR! I know it was a lot of work to implement it, but we'll have a better alternative now that (at last!) we can poll non-child processes on all three platforms.