Skip to content

Commit c06287e

Browse files
committed
pythongh-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.
1 parent 10ee2d9 commit c06287e

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

Lib/subprocess.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,12 +1921,13 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
19211921

19221922
# Wait for exec to fail or succeed; possibly raising an
19231923
# exception (limited in size)
1924-
errpipe_data = bytearray()
1925-
while True:
1926-
part = os.read(errpipe_read, 50000)
1927-
errpipe_data += part
1928-
if not part or len(errpipe_data) > 50000:
1929-
break
1924+
errpipe_data = bytearray(50000)
1925+
unread = memoryview(errpipe_data)
1926+
while count := os.readinto(errpipe_read, unread):
1927+
unread = unread[count:]
1928+
bytes_left = len(unread)
1929+
del unread
1930+
del errpipe_data[-bytes_left:]
19301931
finally:
19311932
# be sure the FD is closed no matter what
19321933
os.close(errpipe_read)

0 commit comments

Comments
 (0)