Skip to content

Commit f129e54

Browse files
committed
Merge pull request #106708 from bruvzg/nohang_exitcode
Fix `execute_with_pipe` / `create_process` exit code.
2 parents d19b34c + 1501f44 commit f129e54

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

drivers/unix/os_unix.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -811,10 +811,14 @@ Dictionary OS_Unix::execute_with_pipe(const String &p_path, const List<String> &
811811
#endif
812812
}
813813

814-
int OS_Unix::_wait_for_pid_completion(const pid_t p_pid, int *r_status, int p_options) {
814+
int OS_Unix::_wait_for_pid_completion(const pid_t p_pid, int *r_status, int p_options, pid_t *r_pid) {
815815
while (true) {
816-
if (waitpid(p_pid, r_status, p_options) != -1) {
816+
pid_t pid = waitpid(p_pid, r_status, p_options);
817+
if (pid != -1) {
817818
// Thread exited normally.
819+
if (r_pid) {
820+
*r_pid = pid;
821+
}
818822
return 0;
819823
}
820824
const int error = errno;
@@ -838,10 +842,14 @@ bool OS_Unix::_check_pid_is_running(const pid_t p_pid, int *r_status) const {
838842
return false;
839843
}
840844

845+
pid_t pid = -1;
841846
int status = 0;
842-
const int result = _wait_for_pid_completion(p_pid, &status, WNOHANG);
847+
const int result = _wait_for_pid_completion(p_pid, &status, WNOHANG, &pid);
843848
if (result == 0) {
844849
// Thread is still running.
850+
if (pi && pid == p_pid) {
851+
pi->exit_code = WIFEXITED(status) ? WEXITSTATUS(status) : status;
852+
}
845853
return true;
846854
}
847855

@@ -852,7 +860,9 @@ bool OS_Unix::_check_pid_is_running(const pid_t p_pid, int *r_status) const {
852860

853861
if (pi) {
854862
pi->is_running = false;
855-
pi->exit_code = status;
863+
if (pid == p_pid) {
864+
pi->exit_code = status;
865+
}
856866
}
857867

858868
if (r_status) {

drivers/unix/os_unix.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class OS_Unix : public OS {
7171
void _load_iconv();
7272
#endif
7373

74-
static int _wait_for_pid_completion(const pid_t p_pid, int *r_status, int p_options);
74+
static int _wait_for_pid_completion(const pid_t p_pid, int *r_status, int p_options, pid_t *r_pid = nullptr);
7575
bool _check_pid_is_running(const pid_t p_pid, int *r_status) const;
7676

7777
protected:

0 commit comments

Comments
 (0)