Skip to content

Commit 3200e87

Browse files
authored
Merge branch '3.14' into backport-5b1862b-3.14
2 parents eeab726 + 4760894 commit 3200e87

4 files changed

Lines changed: 42 additions & 2 deletions

File tree

Doc/library/subprocess.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,9 @@ Instances of the :class:`Popen` class have the following methods:
831831

832832
If the process does not terminate after *timeout* seconds, a
833833
:exc:`TimeoutExpired` exception will be raised. Catching this exception and
834-
retrying communication will not lose any output.
834+
retrying communication will not lose any output. Supplying *input* to a
835+
subsequent post-timeout :meth:`communicate` call is in undefined behavior
836+
and may become an error in the future.
835837

836838
The child process is not killed if the timeout expires, so in order to
837839
cleanup properly a well-behaved application should kill the child process and

Lib/subprocess.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2128,7 +2128,7 @@ def _communicate(self, input, endtime, orig_timeout):
21282128
input_view = self._input.cast("b") # byte input required
21292129

21302130
with _PopenSelector() as selector:
2131-
if self.stdin and input:
2131+
if self.stdin and not self.stdin.closed and self._input:
21322132
selector.register(self.stdin, selectors.EVENT_WRITE)
21332133
if self.stdout and not self.stdout.closed:
21342134
selector.register(self.stdout, selectors.EVENT_READ)

Lib/test/test_subprocess.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,40 @@ def test_wait_negative_timeout(self):
17411741

17421742
self.assertEqual(proc.wait(), 0)
17431743

1744+
def test_post_timeout_communicate_sends_input(self):
1745+
"""GH-141473 regression test; the stdin pipe must close"""
1746+
with subprocess.Popen(
1747+
[sys.executable, "-uc", """\
1748+
import sys
1749+
while c := sys.stdin.read(512):
1750+
sys.stdout.write(c)
1751+
print()
1752+
"""],
1753+
stdin=subprocess.PIPE,
1754+
stdout=subprocess.PIPE,
1755+
stderr=subprocess.PIPE,
1756+
text=True,
1757+
) as proc:
1758+
try:
1759+
data = f"spam{'#'*4096}beans"
1760+
proc.communicate(
1761+
input=data,
1762+
timeout=0,
1763+
)
1764+
except subprocess.TimeoutExpired as exc:
1765+
pass
1766+
# Prior to the bugfix, this would hang as the stdin
1767+
# pipe to the child had not been closed.
1768+
try:
1769+
stdout, stderr = proc.communicate(timeout=15)
1770+
except subprocess.TimeoutExpired as exc:
1771+
self.fail("communicate() hung waiting on child process that should have seen its stdin pipe close and exit")
1772+
self.assertEqual(
1773+
proc.returncode, 0,
1774+
msg=f"STDERR:\n{stderr}\nSTDOUT:\n{stdout}")
1775+
self.assertStartsWith(stdout, "spam")
1776+
self.assertIn("beans", stdout)
1777+
17441778

17451779
class RunFuncTestCase(BaseTestCase):
17461780
def run_python(self, code, **kwargs):
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
When :meth:`subprocess.Popen.communicate` was called with *input* and a
2+
*timeout* and is called for a second time after a
3+
:exc:`~subprocess.TimeoutExpired` exception before the process has died, it
4+
should no longer hang.

0 commit comments

Comments
 (0)