Merge pull request #111058 from timothyqiu/wnohang

Unix: Fix retrieval of PID exit code
This commit is contained in:
Rémi Verschelde 2025-10-06 14:08:03 +02:00
commit 6f0f5a9bd0
No known key found for this signature in database
GPG key ID: C3336907360768E1

View file

@ -815,7 +815,7 @@ int OS_Unix::_wait_for_pid_completion(const pid_t p_pid, int *r_status, int p_op
while (true) {
pid_t pid = waitpid(p_pid, r_status, p_options);
if (pid != -1) {
// Thread exited normally.
// When `p_options` has `WNOHANG`, 0 can be returned if the process is still running.
if (r_pid) {
*r_pid = pid;
}
@ -845,25 +845,20 @@ bool OS_Unix::_check_pid_is_running(const pid_t p_pid, int *r_status) const {
pid_t pid = -1;
int status = 0;
const int result = _wait_for_pid_completion(p_pid, &status, WNOHANG, &pid);
if (result == 0) {
if (result == 0 && pid == 0) {
// Thread is still running.
if (pi && pid == p_pid) {
pi->exit_code = WIFEXITED(status) ? WEXITSTATUS(status) : status;
}
return true;
}
ERR_FAIL_COND_V_MSG(result == -1, false, vformat("Thread %d exited with errno: %d", (int)p_pid, errno));
// Thread exited normally.
ERR_FAIL_COND_V_MSG(result != 0, false, vformat("Thread %d exited with errno: %d", (int)p_pid, errno));
// Thread exited normally.
status = WIFEXITED(status) ? WEXITSTATUS(status) : status;
if (pi) {
pi->is_running = false;
if (pid == p_pid) {
pi->exit_code = status;
}
}
if (r_status) {
*r_status = status;