Skip to content

Commit 76a2622

Browse files
committed
Add a regression test.
1 parent 9bb7516 commit 76a2622

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Lib/test/test_subprocess.py

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

16441644
self.assertEqual(proc.wait(), 0)
16451645

1646+
def test_post_timeout_communicate_sends_input(self):
1647+
"""GH-141473 regression test; the stdin pipe must close"""
1648+
with subprocess.Popen(
1649+
[sys.executable, "-uc", """\
1650+
import sys
1651+
while c := sys.stdin.read(512):
1652+
sys.stdout.write(c)
1653+
print()
1654+
"""],
1655+
stdin=subprocess.PIPE,
1656+
stdout=subprocess.PIPE,
1657+
stderr=subprocess.PIPE,
1658+
text=True,
1659+
) as proc:
1660+
try:
1661+
data = f"spam{'#'*subprocess._PIPE_BUF}beans"
1662+
proc.communicate(
1663+
input=data,
1664+
timeout=0,
1665+
)
1666+
except subprocess.TimeoutExpired as exc:
1667+
pass
1668+
# Prior to the bugfix, this would hang as the stdin
1669+
# pipe to the child had not been closed.
1670+
try:
1671+
stdout, stderr = proc.communicate(timeout=15)
1672+
except subprocess.TimeoutExpired as exc:
1673+
self.fail("communicate() hung waiting on child process that should have seen its stdin pipe close and exit")
1674+
self.assertEqual(
1675+
proc.returncode, 0,
1676+
msg=f"STDERR:\n{stderr}\nSTDOUT:\n{stdout}")
1677+
self.assertStartsWith(stdout, "spam")
1678+
self.assertIn("beans", stdout)
1679+
16461680

16471681
class RunFuncTestCase(BaseTestCase):
16481682
def run_python(self, code, **kwargs):

0 commit comments

Comments
 (0)