Skip to content

Commit 7c5985b

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

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

‎Lib/subprocess.py‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ def __repr__(self):
242242
# so communicate() keeps stdin non-blocking while it writes.
243243
_PIPE_BUF = getattr(select, 'PIPE_BUF', 512)
244244

245+
# A full pipe that is reported as writable gives no event to wait for,
246+
# so communicate() retries the write after a delay that doubles from
247+
# _STDIN_BACKOFF_MIN to _STDIN_BACKOFF_MAX seconds.
248+
_STDIN_BACKOFF_MIN = 0.001
249+
_STDIN_BACKOFF_MAX = 0.05
250+
245251
# poll/select have the advantage of not requiring any extra file
246252
# descriptor, contrarily to epoll/kqueue (also, they require a single
247253
# syscall).
@@ -282,32 +288,52 @@ def _communicate_io_posix(selector, stdin, input_view, input_offset,
282288
- Appends to output_buffers lists in place
283289
"""
284290
stdin_fd = stdin.fileno() if stdin else None
291+
# Seconds to keep stdin out of the selector after a write that
292+
# would have blocked; 0 when stdin is registered or finished.
293+
stdin_backoff = 0
294+
stdin_paused_for = _STDIN_BACKOFF_MIN / 2
285295

286-
while selector.get_map():
296+
while selector.get_map() or stdin_backoff:
287297
remaining = _deadline_remaining(endtime)
288298
if remaining is not None and remaining <= 0:
289299
return (input_offset, False) # Timed out
290300

291-
ready = selector.select(remaining)
301+
if stdin_backoff and (remaining is None
302+
or stdin_backoff < remaining):
303+
ready = selector.select(stdin_backoff)
304+
else:
305+
ready = selector.select(remaining)
292306

293307
# Check timeout after select (may have woken spuriously)
294308
if endtime is not None and _time() > endtime:
295309
return (input_offset, False) # Timed out
296310

311+
if stdin_backoff:
312+
# The wait is over or the child made progress on another
313+
# pipe. Either way, ask about stdin again.
314+
selector.register(stdin, selectors.EVENT_WRITE)
315+
stdin_paused_for, stdin_backoff = stdin_backoff, 0
316+
297317
for key, events in ready:
298318
if key.fd == stdin_fd:
299319
chunk = input_view[input_offset:input_offset + _PIPE_BUF]
300320
try:
301321
input_offset += os.write(key.fd, chunk)
302322
except BlockingIOError:
303-
pass # Spurious readiness: wait for the next select.
323+
# The pipe was reported writable but is full, and
324+
# will be reported writable again right away. Stop
325+
# asking for a while instead of spinning.
326+
selector.unregister(key.fd)
327+
stdin_backoff = min(stdin_paused_for * 2,
328+
_STDIN_BACKOFF_MAX)
304329
except BrokenPipeError:
305330
selector.unregister(key.fd)
306331
try:
307332
stdin.close()
308333
except BrokenPipeError:
309334
pass
310335
else:
336+
stdin_paused_for = _STDIN_BACKOFF_MIN / 2
311337
if input_offset >= len(input_view):
312338
selector.unregister(key.fd)
313339
try:

0 commit comments

Comments
 (0)