From 28f66147e1b4258c41b87a8d799f80904459f977 Mon Sep 17 00:00:00 2001 From: Albert Zeyer Date: Sun, 7 Sep 2025 09:18:28 +0200 Subject: [PATCH] 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 --- Lib/multiprocessing/popen_spawn_posix.py | 4 ++++ .../Library/2024-05-13-09-50-31.gh-issue-118981.zgOQPv.rst | 2 ++ 2 files changed, 6 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-05-13-09-50-31.gh-issue-118981.zgOQPv.rst 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.