Skip to content

Safer cleanup on SIGTERM#366

Merged
gaborcsardi merged 11 commits into
r-lib:mainfrom
lionel-:safe-cleanup
Apr 23, 2026
Merged

Safer cleanup on SIGTERM#366
gaborcsardi merged 11 commits into
r-lib:mainfrom
lionel-:safe-cleanup

Conversation

@lionel-

@lionel- lionel- commented Apr 25, 2023

Copy link
Copy Markdown
Member

This makes the SIGTERM handler safer because system() is not safe to call from a signal handler (see https://sourceware.org/bugzilla/show_bug.cgi?id=4737). To avoid doing too much work in the handler, we prepare a cleanup process ahead of time that we can trigger from the SIGTERM handler with an async-safe one-byte write.

I'm a bit uneasy about calling rm -rf on an envvar though. Should we use the previous approach of querying tempdir() to create an rm -rf command ahead of time?

@lionel- lionel- mentioned this pull request Apr 27, 2023
@lionel- lionel- requested a review from gaborcsardi April 27, 2023 11:10
Replace the popen()-based approach with fork()+execv("/bin/rm") called
directly from the signal handler. Both are async-signal-safe, and this
avoids the persistent child process per R session as well as the
envvar-expansion-at-runtime concern (R_SESSION_TMPDIR is now captured
into a static buffer at handler installation time).

Drop tests that verified the persistent child process lifecycle, and add
a poll_until() in the process-tree test to account for the rm child now
being spawned at signal time rather than pre-started.
Seem hard to make them work.
Instead of reimplementing it.
'poll' refers to the Unix system call in the processx
codebase.
@gaborcsardi

Copy link
Copy Markdown
Member

Sorry for the long wait! I finally bit the bullet on this, but I used a different approach, because running an extra process does not seem right to me. fork() + exec() should be safe from a signal handler, except from some rare edge cases, e.g. pthread_atfork().

@gaborcsardi gaborcsardi merged commit 2198f86 into r-lib:main Apr 23, 2026
21 checks passed
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