]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-118981: multiprocessing.popen_spawn_posix, fix potential hang (gh-118982...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 7 Sep 2025 07:41:54 +0000 (09:41 +0200)
committerGitHub <noreply@github.com>
Sun, 7 Sep 2025 07:41:54 +0000 (07:41 +0000)
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).
(cherry picked from commit 8ed5a2b56cc6a8635e586c641b0b837669f6677b)

Co-authored-by: Albert Zeyer <albzey@gmail.com>
Lib/multiprocessing/popen_spawn_posix.py
Misc/NEWS.d/next/Library/2024-05-13-09-50-31.gh-issue-118981.zgOQPv.rst [new file with mode: 0644]

index 24b8634523e5f2c29cd8bb21022c26d22a4fb13b..cccd659ae776377921d36dc17cd12ec8cd24fee3 100644 (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())
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 (file)
index 0000000..72b9f6c
--- /dev/null
@@ -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.