Encapsulate race-safe process slot synchronization and termination in OS layer for Unix and Windows

This commit is contained in:
Andrea Mazzoleni 2026-09-14 14:55:24 +02:00
parent d7e06271d9
commit 9f48c87dd4
5 changed files with 756 additions and 172 deletions

View file

@ -461,20 +461,41 @@ int main(int argc, char* argv[])
/* unblock signals in parent */
sigprocmask(SIG_SETMASK, &oldset, NULL);
/*
* First wait for child termination without reaping (WNOWAIT).
* The child enters zombie state, holding its PID reserved in the kernel
* so no newly created process can receive the same PID. We clear
* child_pid before the final waitpid() reap so the signal forwarder
* never sends signals to a recycled PID.
*/
siginfo_t info;
int wret;
do {
ret = waitpid(pid, &status, 0);
} while (ret == -1 && errno == EINTR); /* retry if interrupted by signal */
memset(&info, 0, sizeof(info));
wret = waitid(P_PID, (id_t)pid, &info, WEXITED | WNOWAIT);
} while (wret == -1 && errno == EINTR);
if (ret == -1) {
if (wret == -1) {
/* LCOV_EXCL_START */
perror("Failed to wait for SnapRAID");
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
/* clear child PID */
/* clear child PID while PID is still reserved in zombie state */
child_pid = 0;
do {
ret = waitpid(pid, &status, 0);
} while (ret == -1 && errno == EINTR); /* retry if interrupted by signal */
if (ret == -1) {
/* LCOV_EXCL_START */
perror("Failed to reap SnapRAID");
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
/* restore default signal handlers */
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);

View file

@ -309,7 +309,7 @@ static int windows_ps(const char* ps_command, char* out, size_t out_size)
0
};
pid = os_spawn(argv, &stdout_fd, 0, 0);
pid = os_spawn(argv, &stdout_fd, 0, 0, 0);
if (pid < 0) {
log_error(errno, "Failed to run PowerShell command '%s' (from os_spawn).\n", ps_command);
return -1;
@ -337,7 +337,7 @@ static int windows_ps(const char* ps_command, char* out, size_t out_size)
close(stdout_fd);
status = 0;
pid_t pid_ret = os_wait(pid, &status);
pid_t pid_ret = os_wait(pid, &status, 0);
os_dispose(pid);
if (pid_ret < 0) {
log_error(errno, "Failed to wait for PowerShell command '%s' (from os_wait).\n", ps_command);