mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-13 21:44:16 +00:00
Teach keepalives to exit when their parent dies
keepalive processes are typically killed by the main shell process. However if the main shell exits the keepalive may linger. In WSL keepalives are used more often, and the lingering keepalives are both leaks and prevent the tests from finishing. Have keepalives poll for their parent process ID and exit when it changes, so they can clean themselves up. The polling frequency can be low.
This commit is contained in:
parent
14880ce7d1
commit
080521071f
3 changed files with 17 additions and 1 deletions
|
@ -586,12 +586,13 @@ void exec_job(parser_t &parser, job_t *j) {
|
|||
|
||||
if (needs_keepalive) {
|
||||
// Call fork. No need to wait for threads since our use is confined and simple.
|
||||
pid_t parent_pid = getpid();
|
||||
keepalive.pid = execute_fork(false);
|
||||
if (keepalive.pid == 0) {
|
||||
// Child
|
||||
keepalive.pid = getpid();
|
||||
child_set_group(j, &keepalive);
|
||||
pause();
|
||||
run_as_keepalive(parent_pid);
|
||||
exit_without_destructors(0);
|
||||
} else {
|
||||
// Parent
|
||||
|
|
|
@ -546,3 +546,15 @@ bool do_builtin_io(const char *out, size_t outlen, const char *err, size_t errle
|
|||
errno = saved_errno;
|
||||
return success;
|
||||
}
|
||||
|
||||
void run_as_keepalive(pid_t parent_pid) {
|
||||
// Run this process as a keepalive. In typical usage the parent process will kill us. However
|
||||
// this may not happen if the parent process exits abruptly, either via kill or exec. What we do
|
||||
// is poll our ppid() and exit when it differs from parent_pid. We can afford to do this with
|
||||
// low frequency because in the great majority of cases, fish will kill(9) us.
|
||||
for (;;) {
|
||||
// Note sleep is async-safe.
|
||||
if (sleep(1)) break;
|
||||
if (getppid() != parent_pid) break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,6 +47,9 @@ bool do_builtin_io(const char *out, size_t outlen, const char *err, size_t errle
|
|||
void safe_report_exec_error(int err, const char *actual_cmd, const char *const *argv,
|
||||
const char *const *envv);
|
||||
|
||||
/// Runs the process as a keepalive, until the parent process given by parent_pid exits.
|
||||
void run_as_keepalive(pid_t parent_pid);
|
||||
|
||||
#if FISH_USE_POSIX_SPAWN
|
||||
/// Initializes and fills in a posix_spawnattr_t; on success, the caller should destroy it via
|
||||
/// posix_spawnattr_destroy.
|
||||
|
|
Loading…
Reference in a new issue