gh-87512: Fix subprocess using timeout= on Windows blocking with a large input=#142058
Merged
gpshead merged 3 commits intopython:mainfrom Nov 29, 2025
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
87c0ac5 to
bdc28cc
Compare
Add test_communicate_timeout_large_input to verify that TimeoutExpired is raised promptly when communicate() is called with large input and a timeout, even when the subprocess doesn't consume stdin quickly. This test currently passes on POSIX (where select() is used) but is expected to fail on Windows where the stdin write blocks without checking the timeout. The test sends 128KB of input (larger than typical pipe buffers) to a subprocess that sleeps for 3 seconds before reading. With a 0.5s timeout, TimeoutExpired should be raised within a couple seconds, not after the subprocess finishes sleeping. After timeout, the test verifies that input continuation works by calling communicate() again and checking all data was received (testing the fix from pythongh-141473). Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
bdc28cc to
9b7fe29
Compare
subprocess using timeout= on Windows can block when a large input= is supplied
subprocess using timeout= on Windows can block when a large input= is suppliedsubprocess using timeout= on Windows blocking with a large input=
f05d0aa to
7d55f53
Compare
…arge stdin On Windows, Popen._communicate() previously wrote to stdin synchronously, which could block indefinitely if the subprocess didn't consume input quickly and the pipe buffer filled up. The timeout parameter was only checked when joining the reader threads, not during the stdin write. This change moves the stdin writing to a background thread (similar to how stdout/stderr are read in threads), allowing the timeout to be properly enforced. If timeout expires, TimeoutExpired is raised promptly and the writer thread continues in the background. Subsequent calls to communicate() will join the existing writer thread. Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
20d48a9 to
908fd43
Compare
|
🤖 New build scheduled with the buildbot fleet by @gpshead for commit 908fd43 🤖 Results will be shown at: https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F142058%2Fmerge If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again. |
|
Thanks @gpshead for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14. |
|
Thanks @gpshead for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13. |
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this pull request
Nov 29, 2025
… with a large `input=` (pythonGH-142058) On Windows, Popen._communicate() previously wrote to stdin synchronously, which could block indefinitely if the subprocess didn't consume input= quickly and the pipe buffer filled up. The timeout= parameter was only checked when joining the reader threads, not during the stdin write. This change moves the Windows stdin writing to a background thread (similar to how stdout/stderr are read in threads), allowing the timeout to be properly enforced. If timeout expires, TimeoutExpired is raised promptly and the writer thread continues in the background. Subsequent calls to communicate() will join the existing writer thread. Adds test_communicate_timeout_large_input to verify that TimeoutExpired is raised promptly when communicate() is called with large input and a timeout, even when the subprocess doesn't consume stdin quickly. This test already passed on POSIX (where select() is used) but failed on Windows where the stdin write blocks without checking the timeout. (cherry picked from commit 5b1862b) Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
|
GH-142068 is a backport of this pull request to the 3.14 branch. |
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this pull request
Nov 29, 2025
… with a large `input=` (pythonGH-142058) On Windows, Popen._communicate() previously wrote to stdin synchronously, which could block indefinitely if the subprocess didn't consume input= quickly and the pipe buffer filled up. The timeout= parameter was only checked when joining the reader threads, not during the stdin write. This change moves the Windows stdin writing to a background thread (similar to how stdout/stderr are read in threads), allowing the timeout to be properly enforced. If timeout expires, TimeoutExpired is raised promptly and the writer thread continues in the background. Subsequent calls to communicate() will join the existing writer thread. Adds test_communicate_timeout_large_input to verify that TimeoutExpired is raised promptly when communicate() is called with large input and a timeout, even when the subprocess doesn't consume stdin quickly. This test already passed on POSIX (where select() is used) but failed on Windows where the stdin write blocks without checking the timeout. (cherry picked from commit 5b1862b) Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
|
GH-142069 is a backport of this pull request to the 3.13 branch. |
gpshead
added a commit
that referenced
this pull request
Nov 29, 2025
…g with a large `input=` (GH-142058) (#142068) gh-87512: Fix `subprocess` using `timeout=` on Windows blocking with a large `input=` (GH-142058) On Windows, Popen._communicate() previously wrote to stdin synchronously, which could block indefinitely if the subprocess didn't consume input= quickly and the pipe buffer filled up. The timeout= parameter was only checked when joining the reader threads, not during the stdin write. This change moves the Windows stdin writing to a background thread (similar to how stdout/stderr are read in threads), allowing the timeout to be properly enforced. If timeout expires, TimeoutExpired is raised promptly and the writer thread continues in the background. Subsequent calls to communicate() will join the existing writer thread. Adds test_communicate_timeout_large_input to verify that TimeoutExpired is raised promptly when communicate() is called with large input and a timeout, even when the subprocess doesn't consume stdin quickly. This test already passed on POSIX (where select() is used) but failed on Windows where the stdin write blocks without checking the timeout. (cherry picked from commit 5b1862b) Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
gpshead
added a commit
that referenced
this pull request
Nov 29, 2025
…g with a large `input=` (GH-142058) (#142069) gh-87512: Fix `subprocess` using `timeout=` on Windows blocking with a large `input=` (GH-142058) On Windows, Popen._communicate() previously wrote to stdin synchronously, which could block indefinitely if the subprocess didn't consume input= quickly and the pipe buffer filled up. The timeout= parameter was only checked when joining the reader threads, not during the stdin write. This change moves the Windows stdin writing to a background thread (similar to how stdout/stderr are read in threads), allowing the timeout to be properly enforced. If timeout expires, TimeoutExpired is raised promptly and the writer thread continues in the background. Subsequent calls to communicate() will join the existing writer thread. Adds test_communicate_timeout_large_input to verify that TimeoutExpired is raised promptly when communicate() is called with large input and a timeout, even when the subprocess doesn't consume stdin quickly. This test already passed on POSIX (where select() is used) but failed on Windows where the stdin write blocks without checking the timeout. (cherry picked from commit 5b1862b) Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
StanFromIreland
pushed a commit
to StanFromIreland/cpython
that referenced
this pull request
Dec 6, 2025
… with a large `input=` (pythonGH-142058) On Windows, Popen._communicate() previously wrote to stdin synchronously, which could block indefinitely if the subprocess didn't consume input= quickly and the pipe buffer filled up. The timeout= parameter was only checked when joining the reader threads, not during the stdin write. This change moves the Windows stdin writing to a background thread (similar to how stdout/stderr are read in threads), allowing the timeout to be properly enforced. If timeout expires, TimeoutExpired is raised promptly and the writer thread continues in the background. Subsequent calls to communicate() will join the existing writer thread. Adds test_communicate_timeout_large_input to verify that TimeoutExpired is raised promptly when communicate() is called with large input and a timeout, even when the subprocess doesn't consume stdin quickly. This test already passed on POSIX (where select() is used) but failed on Windows where the stdin write blocks without checking the timeout. Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On Windows,
Popen._communicate()previously wrote to stdin synchronously, which could block indefinitely if the subprocess didn't consumeinput=quickly and the pipe buffer filled up. Thetimeout=parameter was only checked when joining the reader threads, not during the stdin write.This change moves the Windows stdin writing to a background thread (similar to how stdout/stderr are read in threads), allowing the timeout to be properly enforced. If timeout expires, TimeoutExpired is raised promptly and the writer thread continues in the background. Subsequent calls to communicate() will join the existing writer thread.
Test driven development
I first added test_communicate_timeout_large_input to verify that TimeoutExpired is raised promptly when communicate() is called with large input and a timeout, even when the subprocess doesn't consume stdin quickly.
This test already passed on POSIX (where select() is used) but failed on Windows where the stdin write blocks without checking the timeout.