Skip to content

[3.15] gh-162016: Fix communicate() hang when a full stdin pipe is reported writable (GH-162058) - #162410

Merged
gpshead merged 1 commit into
python:3.15from
miss-islington:backport-1cc5802-3.15
Nov 22, 2026
Merged

gpshead merged 1 commit into
python:3.15from
miss-islington:backport-1cc5802-3.15

Conversation

@miss-islington

@miss-islington miss-islington commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

On macOS and FreeBSD, poll() and select() can report a pipe as writable when a write to it would block. communicate() assumed a writable event meant a PIPE_BUF-sized write could not block... this works around that potential deadlock.

Changes:

Popen._communicate() makes stdin non-blocking while _communicate_io_posix() runs and restores blocking mode afterwards if stdin is still open. A resumed communicate() after TimeoutExpired and direct proc.stdin writes behave as before.
_communicate_io_posix() handles BlockingIOError by taking stdin out of the selector for a delay that doubles from 1 ms to 50 ms and resets on the next successful write. A timed retry is needed because the kernel keeps reporting the full pipe as writable, so there is no event to wait for. The other pipes are still serviced during the delay, and activity on them ends it early.

Nothing changes when the kernel reports readiness truthfully. In the affected state, a child that reads stdin but writes no output receives its input more slowly than with a blocking write.


(cherry picked from commit 1cc5802)

Co-authored-by: Johnny Wilson Dougherty johnnydougherty09@gmail.com
Co-authored-by: Johnny Wilson Dougherty 192861341+JohnnyWilson16@users.noreply.github.com
Co-authored-by: Gregory P. Smith greg@krypto.org

…orted writable (pythonGH-162058)

On macOS and FreeBSD, poll() and select() can report a pipe as writable when a write to it would block. communicate() assumed a writable event meant a PIPE_BUF-sized write could not block... this works around that potential deadlock.

Changes:

Popen._communicate() makes stdin non-blocking while _communicate_io_posix() runs and restores blocking mode afterwards if stdin is still open. A resumed communicate() after TimeoutExpired and direct proc.stdin writes behave as before.
_communicate_io_posix() handles BlockingIOError by taking stdin out of the selector for a delay that doubles from 1 ms to 50 ms and resets on the next successful write. A timed retry is needed because the kernel keeps reporting the full pipe as writable, so there is no event to wait for. The other pipes are still serviced during the delay, and activity on them ends it early.

Nothing changes when the kernel reports readiness truthfully. In the affected state, a child that reads stdin but writes no output receives its input more slowly than with a blocking write.

---------
(cherry picked from commit 1cc580249141838a520809c21cd971883b5b10cb)

Co-authored-by: Johnny Wilson Dougherty <johnnydougherty09@gmail.com>
Co-authored-by: Johnny Wilson Dougherty <192861341+JohnnyWilson16@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
@gpshead
gpshead enabled auto-merge (squash) November 22, 2026 00:08
@gpshead
gpshead merged commit c03f8d4 into python:3.15 Nov 22, 2026
56 checks passed
@miss-islington
miss-islington deleted the backport-1cc5802-3.15 branch November 22, 2026 00:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants