Add fallback to kill(pid, sig) when process group signaling returns ESRCH in os_term and os_kill

This commit is contained in:
Andrea Mazzoleni 2026-08-04 11:46:35 +02:00
parent 6b46e08a1f
commit b3fdc8d6a5

View file

@ -1349,7 +1349,17 @@ int os_term(pid_t pid)
* This ensures that SnapRAID and any programs it may have spawned are
* terminated together, preventing orphaned worker processes.
*/
return kill(-pid, SIGTERM);
int ret = kill(-pid, SIGTERM);
/*
* If setpgid(0, 0) has not yet executed in the child, process group -pid
* does not exist and kill() returns ESRCH. Fallback to targeting the process directly.
*/
if (ret < 0 && errno == ESRCH) {
ret = kill(pid, SIGTERM);
}
return ret;
}
int os_kill(pid_t pid)
@ -1360,7 +1370,17 @@ int os_kill(pid_t pid)
}
/* Send SIGKILL signal to the negative PID to target the entire Process Group */
return kill(-pid, SIGKILL);
int ret = kill(-pid, SIGKILL);
/*
* If setpgid(0, 0) has not yet executed in the child, process group -pid
* does not exist and kill() returns ESRCH. Fallback to targeting the process directly.
*/
if (ret < 0 && errno == ESRCH) {
ret = kill(pid, SIGKILL);
}
return ret;
}
int os_spawn_and_wait(const char** argv)