diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp index 33087768e07..5cf50b92ed4 100644 --- a/drivers/unix/os_unix.cpp +++ b/drivers/unix/os_unix.cpp @@ -811,10 +811,14 @@ Dictionary OS_Unix::execute_with_pipe(const String &p_path, const List & #endif } -int OS_Unix::_wait_for_pid_completion(const pid_t p_pid, int *r_status, int p_options) { +int OS_Unix::_wait_for_pid_completion(const pid_t p_pid, int *r_status, int p_options, pid_t *r_pid) { while (true) { - if (waitpid(p_pid, r_status, p_options) != -1) { + pid_t pid = waitpid(p_pid, r_status, p_options); + if (pid != -1) { // Thread exited normally. + if (r_pid) { + *r_pid = pid; + } return 0; } const int error = errno; @@ -838,10 +842,14 @@ bool OS_Unix::_check_pid_is_running(const pid_t p_pid, int *r_status) const { return false; } + pid_t pid = -1; int status = 0; - const int result = _wait_for_pid_completion(p_pid, &status, WNOHANG); + const int result = _wait_for_pid_completion(p_pid, &status, WNOHANG, &pid); if (result == 0) { // Thread is still running. + if (pi && pid == p_pid) { + pi->exit_code = WIFEXITED(status) ? WEXITSTATUS(status) : status; + } return true; } @@ -852,7 +860,9 @@ bool OS_Unix::_check_pid_is_running(const pid_t p_pid, int *r_status) const { if (pi) { pi->is_running = false; - pi->exit_code = status; + if (pid == p_pid) { + pi->exit_code = status; + } } if (r_status) { diff --git a/drivers/unix/os_unix.h b/drivers/unix/os_unix.h index 4aeedf73f04..977ac50b0d7 100644 --- a/drivers/unix/os_unix.h +++ b/drivers/unix/os_unix.h @@ -71,7 +71,7 @@ class OS_Unix : public OS { void _load_iconv(); #endif - static int _wait_for_pid_completion(const pid_t p_pid, int *r_status, int p_options); + static int _wait_for_pid_completion(const pid_t p_pid, int *r_status, int p_options, pid_t *r_pid = nullptr); bool _check_pid_is_running(const pid_t p_pid, int *r_status) const; protected: