From 8b9980561d1edb739284afa6cf35605bd7c930a9 Mon Sep 17 00:00:00 2001 From: Johnny Wilson Dougherty <192861341+JohnnyWilson16@users.noreply.github.com> Date: Sun, 15 Nov 2026 23:24:56 +0530 Subject: [PATCH 1/6] gh-162016: Keep stdin non-blocking in subprocess communicate() On some macOS versions, poll() reports a full pipe as writable. The POSIX communicate() loop then called a blocking os.write() on the child's stdin, which hung past the timeout and could deadlock against a child that was blocked writing to stdout. Set stdin to non-blocking around _communicate_io_posix(), treat BlockingIOError as "not ready yet", and restore blocking mode if stdin is still open, so a resumed communicate() and direct proc.stdin writes work as before. Co-Authored-By: Claude Opus 5.5 --- Lib/subprocess.py | 34 +++++++++++++------ Lib/test/test_subprocess.py | 33 ++++++++++++++++++ ...-11-15-17-30-00.gh-issue-162016.pNbStd.rst | 4 +++ 3 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-15-17-30-00.gh-issue-162016.pNbStd.rst diff --git a/Lib/subprocess.py b/Lib/subprocess.py index e2dac4938d57d2..376f7b1713faaf 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -237,9 +237,9 @@ def __repr__(self): __del__ = Close else: - # When select or poll has indicated that the file is writable, - # we can write up to _PIPE_BUF bytes without risk of blocking. - # POSIX defines PIPE_BUF as >= 512. + # Write at most _PIPE_BUF bytes per writable event. POSIX defines + # PIPE_BUF as >= 512. Some platforms report a full pipe as writable, + # so communicate() keeps stdin non-blocking while it writes. _PIPE_BUF = getattr(select, 'PIPE_BUF', 512) # poll/select have the advantage of not requiring any extra file @@ -299,6 +299,8 @@ def _communicate_io_posix(selector, stdin, input_view, input_offset, chunk = input_view[input_offset:input_offset + _PIPE_BUF] try: input_offset += os.write(key.fd, chunk) + except BlockingIOError: + pass # Spurious readiness: wait for the next select. except BrokenPipeError: selector.unregister(key.fd) try: @@ -2430,17 +2432,27 @@ def _communicate(self, input, endtime, orig_timeout): stdin_to_write = (self.stdin if self.stdin and self._input and not self.stdin.closed else None) + if stdin_to_write is not None: + # poll() and select() can report a pipe as writable + # when it has no free space (gh-162016). A blocking + # write() would then hang past the timeout. + os.set_blocking(stdin_to_write.fileno(), False) + try: + new_offset, completed = _communicate_io_posix( + selector, + stdin_to_write, + input_view, + input_offset, + self._fileobj2output, + endtime, + close_on_eof=True) + finally: + # Leave stdin as the user expects it after a timeout. + if stdin_to_write and not stdin_to_write.closed: + os.set_blocking(stdin_to_write.fileno(), True) # Persist the returned offset on self so a subsequent # communicate() after a TimeoutExpired resumes mid-input # rather than re-sending bytes the child already consumed. - new_offset, completed = _communicate_io_posix( - selector, - stdin_to_write, - input_view, - input_offset, - self._fileobj2output, - endtime, - close_on_eof=True) if self._input: self._input_offset = new_offset diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index dedd3cc0e9e867..e6a065cd475e4d 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -3590,6 +3590,39 @@ def test_communicate_BrokenPipeError_stdin_close_with_timeout(self): proc.communicate(timeout=999) mock_proc_stdin.close.assert_called_once_with() + def test_communicate_timeout_stdin_spuriously_writable(self): + # gh-162016: poll() can report a pipe as writable when it has no + # free space. communicate() must not then block in write() past + # its timeout. + class AlwaysWritableSelector(subprocess._PopenSelector): + def select(self, timeout=None): + ready = super().select(0.01) + fds = {key.fd for key, _ in ready} + for key in self.get_map().values(): + if (key.events & selectors.EVENT_WRITE + and key.fd not in fds): + ready.append((key, selectors.EVENT_WRITE)) + return ready + + # The child does not read stdin, so the pipe fills up. Without the + # fix, write() blocks until the child exits. + sleep = support.SHORT_TIMEOUT + with mock.patch.object(subprocess, '_PopenSelector', + AlwaysWritableSelector), \ + subprocess.Popen([sys.executable, '-c', + f'import time; time.sleep({sleep})'], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE) as proc: + try: + start = time.monotonic() + with self.assertRaises(subprocess.TimeoutExpired): + proc.communicate(b'x' * support.PIPE_MAX_SIZE, + timeout=0.1) + self.assertLess(time.monotonic() - start, sleep) + self.assertTrue(os.get_blocking(proc.stdin.fileno())) + finally: + proc.kill() + @unittest.skipUnless(_testcapi is not None and hasattr(_testcapi, 'W_STOPCODE'), 'need _testcapi.W_STOPCODE') diff --git a/Misc/NEWS.d/next/Library/2026-11-15-17-30-00.gh-issue-162016.pNbStd.rst b/Misc/NEWS.d/next/Library/2026-11-15-17-30-00.gh-issue-162016.pNbStd.rst new file mode 100644 index 00000000000000..180e0395329064 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-15-17-30-00.gh-issue-162016.pNbStd.rst @@ -0,0 +1,4 @@ +Fix :meth:`subprocess.Popen.communicate` on POSIX blocking past its +*timeout* when :func:`select.poll` reports the child's stdin pipe as +writable while the pipe is full, as seen on some macOS versions. Stdin is +now non-blocking while :meth:`!communicate` writes to it. From 7c5985bf467effcaf69043eff42d4bf0e707d3d4 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sun, 15 Nov 2026 11:44:35 -0800 Subject: [PATCH 2/6] gh-162016: Back off when stdin is falsely reported writable With stdin non-blocking, a pipe that poll() keeps reporting as writable while it is full made communicate() spin: select() returned at once, write() raised BlockingIOError, and the loop repeated, using a full core until the child read its input or the timeout expired. After a write that would block, take stdin out of the selector for a delay that doubles from 1 ms to 50 ms and resets on the next successful write. The other pipes are still serviced during the delay, and any activity on them ends it early, since output from the child suggests it has also consumed input. Measured on macOS 27.2 with the system-wide pipe buffer limit reached, for a child that never reads stdin and communicate(timeout=3): 3.00 s of CPU before this change, 0.01 s after. Co-Authored-By: Claude Fable 5.2 --- Lib/subprocess.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 376f7b1713faaf..ec5a5991ed4f72 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -242,6 +242,12 @@ def __repr__(self): # so communicate() keeps stdin non-blocking while it writes. _PIPE_BUF = getattr(select, 'PIPE_BUF', 512) + # A full pipe that is reported as writable gives no event to wait for, + # so communicate() retries the write after a delay that doubles from + # _STDIN_BACKOFF_MIN to _STDIN_BACKOFF_MAX seconds. + _STDIN_BACKOFF_MIN = 0.001 + _STDIN_BACKOFF_MAX = 0.05 + # poll/select have the advantage of not requiring any extra file # descriptor, contrarily to epoll/kqueue (also, they require a single # syscall). @@ -282,25 +288,44 @@ def _communicate_io_posix(selector, stdin, input_view, input_offset, - Appends to output_buffers lists in place """ stdin_fd = stdin.fileno() if stdin else None + # Seconds to keep stdin out of the selector after a write that + # would have blocked; 0 when stdin is registered or finished. + stdin_backoff = 0 + stdin_paused_for = _STDIN_BACKOFF_MIN / 2 - while selector.get_map(): + while selector.get_map() or stdin_backoff: remaining = _deadline_remaining(endtime) if remaining is not None and remaining <= 0: return (input_offset, False) # Timed out - ready = selector.select(remaining) + if stdin_backoff and (remaining is None + or stdin_backoff < remaining): + ready = selector.select(stdin_backoff) + else: + ready = selector.select(remaining) # Check timeout after select (may have woken spuriously) if endtime is not None and _time() > endtime: return (input_offset, False) # Timed out + if stdin_backoff: + # The wait is over or the child made progress on another + # pipe. Either way, ask about stdin again. + selector.register(stdin, selectors.EVENT_WRITE) + stdin_paused_for, stdin_backoff = stdin_backoff, 0 + for key, events in ready: if key.fd == stdin_fd: chunk = input_view[input_offset:input_offset + _PIPE_BUF] try: input_offset += os.write(key.fd, chunk) except BlockingIOError: - pass # Spurious readiness: wait for the next select. + # The pipe was reported writable but is full, and + # will be reported writable again right away. Stop + # asking for a while instead of spinning. + selector.unregister(key.fd) + stdin_backoff = min(stdin_paused_for * 2, + _STDIN_BACKOFF_MAX) except BrokenPipeError: selector.unregister(key.fd) try: @@ -308,6 +333,7 @@ def _communicate_io_posix(selector, stdin, input_view, input_offset, except BrokenPipeError: pass else: + stdin_paused_for = _STDIN_BACKOFF_MIN / 2 if input_offset >= len(input_view): selector.unregister(key.fd) try: From d56fd9c012efc1eeb42689fb62ed42df96602f42 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sun, 15 Nov 2026 11:49:33 -0800 Subject: [PATCH 3/6] gh-162016: Address review; clarify the NEWS entry Use "is not None" when restoring stdin's blocking mode, matching the check made before the try block. Reword the NEWS entry to say that this works around a macOS and FreeBSD kernel bug that appears under system-wide pipe buffer memory pressure, most likely on loaded hosts with many pipes in use. Co-Authored-By: Claude Fable 5.2 --- Lib/subprocess.py | 2 +- .../2026-11-15-17-30-00.gh-issue-162016.pNbStd.rst | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index ec5a5991ed4f72..d07724fe37af99 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -2474,7 +2474,7 @@ def _communicate(self, input, endtime, orig_timeout): close_on_eof=True) finally: # Leave stdin as the user expects it after a timeout. - if stdin_to_write and not stdin_to_write.closed: + if stdin_to_write is not None and not stdin_to_write.closed: os.set_blocking(stdin_to_write.fileno(), True) # Persist the returned offset on self so a subsequent # communicate() after a TimeoutExpired resumes mid-input diff --git a/Misc/NEWS.d/next/Library/2026-11-15-17-30-00.gh-issue-162016.pNbStd.rst b/Misc/NEWS.d/next/Library/2026-11-15-17-30-00.gh-issue-162016.pNbStd.rst index 180e0395329064..5a006ba7b62b40 100644 --- a/Misc/NEWS.d/next/Library/2026-11-15-17-30-00.gh-issue-162016.pNbStd.rst +++ b/Misc/NEWS.d/next/Library/2026-11-15-17-30-00.gh-issue-162016.pNbStd.rst @@ -1,4 +1,8 @@ -Fix :meth:`subprocess.Popen.communicate` on POSIX blocking past its -*timeout* when :func:`select.poll` reports the child's stdin pipe as -writable while the pipe is full, as seen on some macOS versions. Stdin is -now non-blocking while :meth:`!communicate` writes to it. +Work around a macOS and FreeBSD kernel bug that could make +:meth:`subprocess.Popen.communicate` hang or block past its *timeout*. +When the operating system is under system-wide pipe buffer memory pressure, +:func:`select.poll` and :func:`select.select` can report the child's stdin +pipe as writable when a write to it would block. This was most likely to be +seen on loaded hosts with many pipes in use at once. :meth:`!communicate` +now keeps stdin non-blocking while it writes to it and retries after a short +delay when the pipe turns out to be full. From 8dc3b415866d15b11b3655869ccda59c3668fd09 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sun, 15 Nov 2026 11:58:04 -0800 Subject: [PATCH 4/6] gh-162016: Shorten the NEWS entry Lead with the affected API and what a user would notice, and drop the description of how the workaround is implemented. Co-Authored-By: Claude Fable 5.2 --- .../2026-11-15-17-30-00.gh-issue-162016.pNbStd.rst | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2026-11-15-17-30-00.gh-issue-162016.pNbStd.rst b/Misc/NEWS.d/next/Library/2026-11-15-17-30-00.gh-issue-162016.pNbStd.rst index 5a006ba7b62b40..5fa107c8c64271 100644 --- a/Misc/NEWS.d/next/Library/2026-11-15-17-30-00.gh-issue-162016.pNbStd.rst +++ b/Misc/NEWS.d/next/Library/2026-11-15-17-30-00.gh-issue-162016.pNbStd.rst @@ -1,8 +1,4 @@ -Work around a macOS and FreeBSD kernel bug that could make -:meth:`subprocess.Popen.communicate` hang or block past its *timeout*. -When the operating system is under system-wide pipe buffer memory pressure, -:func:`select.poll` and :func:`select.select` can report the child's stdin -pipe as writable when a write to it would block. This was most likely to be -seen on loaded hosts with many pipes in use at once. :meth:`!communicate` -now keeps stdin non-blocking while it writes to it and retries after a short -delay when the pipe turns out to be full. +Fix :meth:`subprocess.Popen.communicate` hanging or blocking past its +*timeout* while writing to the child's stdin on macOS and FreeBSD. This +works around a kernel bug most likely seen on loaded hosts with many pipes +in active use at once. From a9e5ef9225df4fb5b67c1c2b1d70b4f9ced09e96 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sun, 15 Nov 2026 12:03:01 -0800 Subject: [PATCH 5/6] gh-162016: Make comments stand on their own Explain what stdin_paused_for holds, say why stdin's blocking mode is restored in terms of what later callers expect, and describe the test's blocking case without referring to "the fix". Co-Authored-By: Claude Fable 5.2 --- Lib/subprocess.py | 5 ++++- Lib/test/test_subprocess.py | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index d07724fe37af99..3e9f58fb1f4f76 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -291,6 +291,8 @@ def _communicate_io_posix(selector, stdin, input_view, input_offset, # Seconds to keep stdin out of the selector after a write that # would have blocked; 0 when stdin is registered or finished. stdin_backoff = 0 + # The previous such delay, which the next one doubles. A + # successful write resets it. stdin_paused_for = _STDIN_BACKOFF_MIN / 2 while selector.get_map() or stdin_backoff: @@ -2473,7 +2475,8 @@ def _communicate(self, input, endtime, orig_timeout): endtime, close_on_eof=True) finally: - # Leave stdin as the user expects it after a timeout. + # Code that uses self.stdin after communicate() returns + # or times out expects writes to it to block. if stdin_to_write is not None and not stdin_to_write.closed: os.set_blocking(stdin_to_write.fileno(), True) # Persist the returned offset on self so a subsequent diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index e6a065cd475e4d..68eedc4a6185c2 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -3604,8 +3604,8 @@ def select(self, timeout=None): ready.append((key, selectors.EVENT_WRITE)) return ready - # The child does not read stdin, so the pipe fills up. Without the - # fix, write() blocks until the child exits. + # The child does not read stdin, so the pipe fills up. A blocking + # write() would not return until the child exits. sleep = support.SHORT_TIMEOUT with mock.patch.object(subprocess, '_PopenSelector', AlwaysWritableSelector), \ From 2faa9c8431fb9fa2884db5fc52a0f588c00953ad Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sun, 15 Nov 2026 12:22:11 -0800 Subject: [PATCH 6/6] gh-162016: Test that communicate() does not spin or lose input The test selector waited 10 ms in every select() call, which hid a busy loop. It now reports stdin as writable without waiting and counts how often it did so for a pipe that was not, so the timeout test can assert that communicate() does not keep retrying a full pipe. Add a test for the case where stdin is full and is the only pipe left: the child closes stdout before it reads its input, and checks that it received all of it. Co-Authored-By: Claude Fable 5.2 --- Lib/test/test_subprocess.py | 61 +++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 68eedc4a6185c2..23359d3147ade3 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -3590,25 +3590,33 @@ def test_communicate_BrokenPipeError_stdin_close_with_timeout(self): proc.communicate(timeout=999) mock_proc_stdin.close.assert_called_once_with() - def test_communicate_timeout_stdin_spuriously_writable(self): - # gh-162016: poll() can report a pipe as writable when it has no - # free space. communicate() must not then block in write() past - # its timeout. + def _spuriously_writable_selector(self): + # A selector that reports stdin as writable whenever it is + # registered. poll() on macOS and FreeBSD can do this for a full + # pipe when many pipes are in use system-wide (gh-162016). class AlwaysWritableSelector(subprocess._PopenSelector): + spurious_events = 0 + def select(self, timeout=None): - ready = super().select(0.01) - fds = {key.fd for key, _ in ready} + ready = super().select(0) + writable = {key.fd for key, events in ready + if events & selectors.EVENT_WRITE} for key in self.get_map().values(): if (key.events & selectors.EVENT_WRITE - and key.fd not in fds): + and key.fd not in writable): ready.append((key, selectors.EVENT_WRITE)) - return ready + type(self).spurious_events += 1 + return ready or super().select(timeout) - # The child does not read stdin, so the pipe fills up. A blocking - # write() would not return until the child exits. + return AlwaysWritableSelector + + def test_communicate_timeout_stdin_spuriously_writable(self): + # The child never reads stdin, so the pipe fills up. communicate() + # must neither block in write() past its timeout nor spin on a pipe + # that keeps being reported as writable. + selector = self._spuriously_writable_selector() sleep = support.SHORT_TIMEOUT - with mock.patch.object(subprocess, '_PopenSelector', - AlwaysWritableSelector), \ + with mock.patch.object(subprocess, '_PopenSelector', selector), \ subprocess.Popen([sys.executable, '-c', f'import time; time.sleep({sleep})'], stdin=subprocess.PIPE, @@ -3617,12 +3625,39 @@ def select(self, timeout=None): start = time.monotonic() with self.assertRaises(subprocess.TimeoutExpired): proc.communicate(b'x' * support.PIPE_MAX_SIZE, - timeout=0.1) + timeout=0.5) self.assertLess(time.monotonic() - start, sleep) + # communicate() must wait between attempts to write to a + # full pipe. Retrying in a busy loop would produce many + # thousands of these events within the timeout. + self.assertLess(selector.spurious_events, 100) self.assertTrue(os.get_blocking(proc.stdin.fileno())) finally: proc.kill() + def test_communicate_stdin_spuriously_writable_last_pipe(self): + # All input must still arrive when stdin is full, is reported as + # writable, and is the only pipe communicate() has left to service. + selector = self._spuriously_writable_selector() + data = bytes(range(256)) * (support.PIPE_MAX_SIZE // 256) + code = textwrap.dedent(f""" + import os, sys, time + os.close(1) + time.sleep(0.1) # Let the parent fill the stdin pipe. + data = bytes(range(256)) * {len(data) // 256} + sys.exit(sys.stdin.buffer.read() != data) + """) + with mock.patch.object(subprocess, '_PopenSelector', selector), \ + subprocess.Popen([sys.executable, '-c', code], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE) as proc: + try: + proc.communicate(data, timeout=support.SHORT_TIMEOUT) + self.assertEqual(proc.returncode, 0) + self.assertGreater(selector.spurious_events, 0) + finally: + proc.kill() + @unittest.skipUnless(_testcapi is not None and hasattr(_testcapi, 'W_STOPCODE'), 'need _testcapi.W_STOPCODE')