Skip to content

Commit 8ed5a2b

Browse files
authored
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).
1 parent c117b03 commit 8ed5a2b

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

Lib/multiprocessing/popen_spawn_posix.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ def _launch(self, process_obj):
5757
self._fds.extend([child_r, child_w])
5858
self.pid = util.spawnv_passfds(spawn.get_executable(),
5959
cmd, self._fds)
60+
os.close(child_r)
61+
child_r = None
62+
os.close(child_w)
63+
child_w = None
6064
self.sentinel = parent_r
6165
with open(parent_w, 'wb', closefd=False) as f:
6266
f.write(fp.getbuffer())
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix potential hang in ``multiprocessing.popen_spawn_posix`` that can happen
2+
when the child proc dies early by closing the child fds right away.

0 commit comments

Comments
 (0)