@@ -815,7 +815,7 @@ int OS_Unix::_wait_for_pid_completion(const pid_t p_pid, int *r_status, int p_op
815815 while (true ) {
816816 pid_t pid = waitpid (p_pid, r_status, p_options);
817817 if (pid != -1 ) {
818- // Thread exited normally .
818+ // When `p_options` has `WNOHANG`, 0 can be returned if the process is still running .
819819 if (r_pid) {
820820 *r_pid = pid;
821821 }
@@ -845,24 +845,19 @@ bool OS_Unix::_check_pid_is_running(const pid_t p_pid, int *r_status) const {
845845 pid_t pid = -1 ;
846846 int status = 0 ;
847847 const int result = _wait_for_pid_completion (p_pid, &status, WNOHANG, &pid);
848- if (result == 0 ) {
848+ if (result == 0 && pid == 0 ) {
849849 // Thread is still running.
850- if (pi && pid == p_pid) {
851- pi->exit_code = WIFEXITED (status) ? WEXITSTATUS (status) : status;
852- }
853850 return true ;
854851 }
855852
856- ERR_FAIL_COND_V_MSG (result == -1 , false , vformat (" Thread %d exited with errno: %d" , (int )p_pid, errno));
857- // Thread exited normally.
853+ ERR_FAIL_COND_V_MSG (result != 0 , false , vformat (" Thread %d exited with errno: %d" , (int )p_pid, errno));
858854
855+ // Thread exited normally.
859856 status = WIFEXITED (status) ? WEXITSTATUS (status) : status;
860857
861858 if (pi) {
862859 pi->is_running = false ;
863- if (pid == p_pid) {
864- pi->exit_code = status;
865- }
860+ pi->exit_code = status;
866861 }
867862
868863 if (r_status) {
0 commit comments