From c06287eab5c09330af0c867b04e8287b556234b1 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Thu, 30 Jan 2025 20:50:24 -0800 Subject: [PATCH 1/2] gh-129205: Use os.readinto() in subprocess errpipe_read Read into a pre-allocated fixed size buffer. The previous code the buffer could actually get to 100_000 bytes in two reads (first read returns 50_000, second pass through loop gets another 50_000), so this does change behavior. I think the fixed length of 50_000 was the intention though. This is used to pass exception issues that happen during _fork_exec from the child to parent process. --- Lib/subprocess.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 2044d2a42897e9..28edfc1de5b079 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -1921,12 +1921,13 @@ def _execute_child(self, args, executable, preexec_fn, close_fds, # Wait for exec to fail or succeed; possibly raising an # exception (limited in size) - errpipe_data = bytearray() - while True: - part = os.read(errpipe_read, 50000) - errpipe_data += part - if not part or len(errpipe_data) > 50000: - break + errpipe_data = bytearray(50000) + unread = memoryview(errpipe_data) + while count := os.readinto(errpipe_read, unread): + unread = unread[count:] + bytes_left = len(unread) + del unread + del errpipe_data[-bytes_left:] finally: # be sure the FD is closed no matter what os.close(errpipe_read) From c4cb414a1bc6b7f2de94c809b81dfd2a770fe5ed Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 2 Feb 2025 20:55:21 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-02-02-20-55-18.gh-issue-129205.hb1iMy.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-02-02-20-55-18.gh-issue-129205.hb1iMy.rst diff --git a/Misc/NEWS.d/next/Library/2025-02-02-20-55-18.gh-issue-129205.hb1iMy.rst b/Misc/NEWS.d/next/Library/2025-02-02-20-55-18.gh-issue-129205.hb1iMy.rst new file mode 100644 index 00000000000000..70e028a7c7fa38 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-02-02-20-55-18.gh-issue-129205.hb1iMy.rst @@ -0,0 +1 @@ +:mod:`subprocess` forkserver now uses a fixed 50,000 byte buffer to read startup errors from the child processes it runs. Previously there could be up to 100,000 bytes read with multiple allocations and copies.