gh-118981: multiprocessing.popen_spawn_posix, fix potential hang (gh-118982)

fix potential hang.

It can happen that the child crashes right in the beginning for whatever reason. In this case, the parent will hang when writing into the pipe, because the child fd is not closed yet.

The normal pattern is to close the child fds right after the child proc is forked/executed/spawned, so when the child dies, then also the pipes will be closed, and there will be no hang (the parent gets SIGPIPE instead).
This commit is contained in:
Albert Zeyer
2025-09-07 09:18:28 +02:00
committed by GitHub
parent c117b03385
commit 8ed5a2b56c
2 changed files with 6 additions and 0 deletions

View File

@@ -57,6 +57,10 @@ class Popen(popen_fork.Popen):
self._fds.extend([child_r, child_w])
self.pid = util.spawnv_passfds(spawn.get_executable(),
cmd, self._fds)
os.close(child_r)
child_r = None
os.close(child_w)
child_w = None
self.sentinel = parent_r
with open(parent_w, 'wb', closefd=False) as f:
f.write(fp.getbuffer())

View File

@@ -0,0 +1,2 @@
Fix potential hang in ``multiprocessing.popen_spawn_posix`` that can happen
when the child proc dies early by closing the child fds right away.