Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kill with grace #367

Open
wants to merge 7 commits into
base: safe-cleanup
Choose a base branch
from
Open

Kill with grace #367

wants to merge 7 commits into from

Conversation

lionel-
Copy link
Member

@lionel- lionel- commented Apr 27, 2023

Branched from #366.

  • Revive the grace argument in the kill() method. We now first send a SIGTERM, wait for a grace period, and only then SIGKILL

  • To avoid a delay of grace seconds when the process terminates quickly, we poll for termination. This is supported by the same mechanism that is already implemented for wait(). The routine is split into an R-facing processx_wait() function and a C-facing c_processx_wait() variant which is called by the kill implementation.

  • The new() and run() methods of processes gain a cleanup_grace argument that is passed to kill() and kill_tree() on termination.

#include <signal.h>

void R_init_sigtermignore(DllInfo *dll) {
signal(SIGTERM, SIG_IGN);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I wrote this branch I've added sigterm handlers to px in r-lib/ps#149. We could use this instead of this client lib for our unit tests by depending on the dev version of ps.

.grace = handle->cleanup_grace,
.name = R_NilValue
};
r_with_cleanup_context(c_processx_kill_data, &data);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to use the non-jumpy approach implemented in the ps PR, but I went for minimal changes here.


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__block_sigchld();
sigset_t old;
processx__block_sigchld_save(&old);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're now restoring the caller's signal mask on exit since this can now be called from C.

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.

1 participant