Configure PR_SET_PDEATHSIG on Linux and Job Objects on Windows to prevent orphaned subprocesses on program crash
Some checks are pending
Build / build-mac (push) Waiting to run
Build / build-linux (push) Waiting to run
Build / build-win (push) Waiting to run
CodeQL Advanced / Analyze (c-cpp) (push) Waiting to run
CodeQL Advanced / Analyze (actions) (push) Waiting to run

This commit is contained in:
Andrea Mazzoleni 2026-08-01 18:26:01 +02:00
parent 9a1987e024
commit cf2196a90f
4 changed files with 70 additions and 10 deletions

View file

@ -2531,6 +2531,8 @@ static int argcat(WCHAR* cmd, int size, int pos, const WCHAR* arg)
return pos;
}
static HANDLE os_job_handle = 0;
pid_t os_spawn(char** argv, int* stdout_read_int, int* stderr_read_int, const char* run_as_user)
{
wchar_t conv[CONV_MAX];
@ -2743,6 +2745,14 @@ pid_t os_spawn(char** argv, int* stdout_read_int, int* stderr_read_int, const ch
return -1;
}
/*
* Assign spawned process to Job Object configured with JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE.
* This ensures Windows automatically terminates child processes if the daemon crashes unexpectedly.
*/
if (os_job_handle != 0) {
AssignProcessToJobObject(os_job_handle, pi.hProcess);
}
/* close the write end of the pipes in the parent */
if (has_out) {
CloseHandle(stdout_write_handle);
@ -3531,6 +3541,18 @@ void os_init(unsigned opt)
os_exit();
}
/* initialize job object to terminate child processes if daemon dies */
os_job_handle = CreateJobObjectW(0, 0);
if (os_job_handle != 0) {
JOBOBJECT_EXTENDED_LIMIT_INFORMATION info;
ZeroMemory(&info, sizeof(info));
info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
if (!SetInformationJobObject(os_job_handle, JobObjectExtendedLimitInformation, &info, sizeof(info))) {
CloseHandle(os_job_handle);
os_job_handle = 0;
}
}
/* check for Wine presence */
is_wine = GetProcAddress(ntdll, "wine_get_version") != 0;
@ -3553,6 +3575,11 @@ void os_init(unsigned opt)
void os_done(void)
{
if (os_job_handle != 0) {
CloseHandle(os_job_handle);
os_job_handle = 0;
}
/* delete the thread local storage for strerror() */
windows_key_delete(last_error);