Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-35182: fix communicate() crash after child closes its pipes #17020

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/subprocess.py
Expand Up @@ -1950,9 +1950,9 @@ def _communicate(self, input, endtime, orig_timeout):
with _PopenSelector() as selector:
if self.stdin and input:
selector.register(self.stdin, selectors.EVENT_WRITE)
if self.stdout:
if self.stdout and not self.stdout.closed:
selector.register(self.stdout, selectors.EVENT_READ)
if self.stderr:
if self.stderr and not self.stderr.closed:
selector.register(self.stderr, selectors.EVENT_READ)

while selector.get_map():
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_subprocess.py
Expand Up @@ -3105,6 +3105,16 @@ def test_stopped(self):

self.assertEqual(returncode, -3)

def test_communicate_repeated_call_after_stdout_close(self):
proc = subprocess.Popen([sys.executable, '-c',
'import os, time; os.close(1), time.sleep(2)'],
stdout=subprocess.PIPE)
while True:
try:
proc.communicate(timeout=0.1)
return
except subprocess.TimeoutExpired:
pass

@unittest.skipUnless(mswindows, "Windows specific tests")
class Win32ProcessTestCase(BaseTestCase):
Expand Down
@@ -0,0 +1,3 @@
Fixed :func:`Popen.communicate` subsequent call crash when the child process
has already closed any piped standard stream, but still continues to be
running. Patch by Andriy Maletsky.