Skip to content

Commit 8b99805

Browse files
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 <noreply@anthropic.com>
1 parent 369d8bc commit 8b99805

3 files changed

Lines changed: 60 additions & 11 deletions

File tree

‎Lib/subprocess.py‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,9 @@ def __repr__(self):
237237

238238
__del__ = Close
239239
else:
240-
# When select or poll has indicated that the file is writable,
241-
# we can write up to _PIPE_BUF bytes without risk of blocking.
242-
# POSIX defines PIPE_BUF as >= 512.
240+
# Write at most _PIPE_BUF bytes per writable event. POSIX defines
241+
# PIPE_BUF as >= 512. Some platforms report a full pipe as writable,
242+
# so communicate() keeps stdin non-blocking while it writes.
243243
_PIPE_BUF = getattr(select, 'PIPE_BUF', 512)
244244

245245
# 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,
299299
chunk = input_view[input_offset:input_offset + _PIPE_BUF]
300300
try:
301301
input_offset += os.write(key.fd, chunk)
302+
except BlockingIOError:
303+
pass # Spurious readiness: wait for the next select.
302304
except BrokenPipeError:
303305
selector.unregister(key.fd)
304306
try:
@@ -2430,17 +2432,27 @@ def _communicate(self, input, endtime, orig_timeout):
24302432

24312433
stdin_to_write = (self.stdin if self.stdin and self._input
24322434
and not self.stdin.closed else None)
2435+
if stdin_to_write is not None:
2436+
# poll() and select() can report a pipe as writable
2437+
# when it has no free space (gh-162016). A blocking
2438+
# write() would then hang past the timeout.
2439+
os.set_blocking(stdin_to_write.fileno(), False)
2440+
try:
2441+
new_offset, completed = _communicate_io_posix(
2442+
selector,
2443+
stdin_to_write,
2444+
input_view,
2445+
input_offset,
2446+
self._fileobj2output,
2447+
endtime,
2448+
close_on_eof=True)
2449+
finally:
2450+
# Leave stdin as the user expects it after a timeout.
2451+
if stdin_to_write and not stdin_to_write.closed:
2452+
os.set_blocking(stdin_to_write.fileno(), True)
24332453
# Persist the returned offset on self so a subsequent
24342454
# communicate() after a TimeoutExpired resumes mid-input
24352455
# rather than re-sending bytes the child already consumed.
2436-
new_offset, completed = _communicate_io_posix(
2437-
selector,
2438-
stdin_to_write,
2439-
input_view,
2440-
input_offset,
2441-
self._fileobj2output,
2442-
endtime,
2443-
close_on_eof=True)
24442456
if self._input:
24452457
self._input_offset = new_offset
24462458

‎Lib/test/test_subprocess.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,6 +3590,39 @@ def test_communicate_BrokenPipeError_stdin_close_with_timeout(self):
35903590
proc.communicate(timeout=999)
35913591
mock_proc_stdin.close.assert_called_once_with()
35923592

3593+
def test_communicate_timeout_stdin_spuriously_writable(self):
3594+
# gh-162016: poll() can report a pipe as writable when it has no
3595+
# free space. communicate() must not then block in write() past
3596+
# its timeout.
3597+
class AlwaysWritableSelector(subprocess._PopenSelector):
3598+
def select(self, timeout=None):
3599+
ready = super().select(0.01)
3600+
fds = {key.fd for key, _ in ready}
3601+
for key in self.get_map().values():
3602+
if (key.events & selectors.EVENT_WRITE
3603+
and key.fd not in fds):
3604+
ready.append((key, selectors.EVENT_WRITE))
3605+
return ready
3606+
3607+
# The child does not read stdin, so the pipe fills up. Without the
3608+
# fix, write() blocks until the child exits.
3609+
sleep = support.SHORT_TIMEOUT
3610+
with mock.patch.object(subprocess, '_PopenSelector',
3611+
AlwaysWritableSelector), \
3612+
subprocess.Popen([sys.executable, '-c',
3613+
f'import time; time.sleep({sleep})'],
3614+
stdin=subprocess.PIPE,
3615+
stdout=subprocess.PIPE) as proc:
3616+
try:
3617+
start = time.monotonic()
3618+
with self.assertRaises(subprocess.TimeoutExpired):
3619+
proc.communicate(b'x' * support.PIPE_MAX_SIZE,
3620+
timeout=0.1)
3621+
self.assertLess(time.monotonic() - start, sleep)
3622+
self.assertTrue(os.get_blocking(proc.stdin.fileno()))
3623+
finally:
3624+
proc.kill()
3625+
35933626
@unittest.skipUnless(_testcapi is not None
35943627
and hasattr(_testcapi, 'W_STOPCODE'),
35953628
'need _testcapi.W_STOPCODE')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix :meth:`subprocess.Popen.communicate` on POSIX blocking past its
2+
*timeout* when :func:`select.poll` reports the child's stdin pipe as
3+
writable while the pipe is full, as seen on some macOS versions. Stdin is
4+
now non-blocking while :meth:`!communicate` writes to it.

0 commit comments

Comments
 (0)