https://github.com/python/cpython/commit/3013683e9962f5f7f0915ed6c94a551dc396b8ac
commit: 3013683e9962f5f7f0915ed6c94a551dc396b8ac
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: encukou <[email protected]>
date: 2025-10-08T14:22:05+02:00
summary:

[3.14] gh-118981: multiprocessing.popen_spawn_posix, fix potential hang 
(gh-118982) (GH-138605)

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).
(cherry picked from commit 8ed5a2b56cc6a8635e586c641b0b837669f6677b)

Co-authored-by: Albert Zeyer <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-05-13-09-50-31.gh-issue-118981.zgOQPv.rst
M Lib/multiprocessing/popen_spawn_posix.py

diff --git a/Lib/multiprocessing/popen_spawn_posix.py 
b/Lib/multiprocessing/popen_spawn_posix.py
index 24b8634523e5f2..cccd659ae77637 100644
--- a/Lib/multiprocessing/popen_spawn_posix.py
+++ b/Lib/multiprocessing/popen_spawn_posix.py
@@ -57,6 +57,10 @@ def _launch(self, process_obj):
             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())
diff --git 
a/Misc/NEWS.d/next/Library/2024-05-13-09-50-31.gh-issue-118981.zgOQPv.rst 
b/Misc/NEWS.d/next/Library/2024-05-13-09-50-31.gh-issue-118981.zgOQPv.rst
new file mode 100644
index 00000000000000..72b9f6c9e4eb99
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-05-13-09-50-31.gh-issue-118981.zgOQPv.rst
@@ -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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to