Skip to content

Commit 6ee771b

Browse files
committed
Use waitpid() + WNOHANG even if process exited to avoid rare race
1 parent 645ef6c commit 6ee771b

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

Lib/subprocess.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2157,7 +2157,13 @@ def _wait(self, timeout):
21572157
if timeout < 0:
21582158
raise TimeoutExpired(self.args, timeout)
21592159
if self._wait_pidfd(timeout) or self._wait_kqueue(timeout):
2160-
self._blocking_wait()
2160+
# At this point os.waitpid(pid, 0) should return
2161+
# immediately, but in rare races another thread or
2162+
# signal handler may have already reaped the PID.
2163+
# Using _busy_wait(0) (WNOHANG) ensures we attempt
2164+
# a non-blocking reap safely without blocking
2165+
# indefinitely.
2166+
self._busy_wait(0)
21612167
else:
21622168
self._busy_wait(timeout)
21632169
else:

0 commit comments

Comments
 (0)