Commit ab900c21 authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #21619: Popen objects no longer leave a zombie after exit in the with

statement if the pipe was broken.  Patch by Martin Panter.
parent fdde79db
...@@ -896,10 +896,12 @@ class Popen(object): ...@@ -896,10 +896,12 @@ class Popen(object):
self.stdout.close() self.stdout.close()
if self.stderr: if self.stderr:
self.stderr.close() self.stderr.close()
if self.stdin: try: # Flushing a BufferedWriter may raise an error
self.stdin.close() if self.stdin:
# Wait for the process to terminate, to avoid zombies. self.stdin.close()
self.wait() finally:
# Wait for the process to terminate, to avoid zombies.
self.wait()
def __del__(self, _maxsize=sys.maxsize): def __del__(self, _maxsize=sys.maxsize):
if not self._child_created: if not self._child_created:
......
...@@ -2521,6 +2521,21 @@ class ContextManagerTests(BaseTestCase): ...@@ -2521,6 +2521,21 @@ class ContextManagerTests(BaseTestCase):
stderr=subprocess.PIPE) as proc: stderr=subprocess.PIPE) as proc:
pass pass
def test_broken_pipe_cleanup(self):
"""Broken pipe error should not prevent wait() (Issue 21619)"""
proc = subprocess.Popen([sys.executable, "-c",
"import sys;"
"sys.stdin.close();"
"sys.stdout.close();" # Signals that input pipe is closed
], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
proc.stdout.read() # Make sure subprocess has closed its input
proc.stdin.write(b"buffered data")
self.assertIsNone(proc.returncode)
self.assertRaises(BrokenPipeError, proc.__exit__, None, None, None)
self.assertEqual(0, proc.returncode)
self.assertTrue(proc.stdin.closed)
self.assertTrue(proc.stdout.closed)
def test_main(): def test_main():
unit_tests = (ProcessTestCase, unit_tests = (ProcessTestCase,
......
...@@ -13,6 +13,9 @@ Core and Builtins ...@@ -13,6 +13,9 @@ Core and Builtins
Library Library
------- -------
- Issue #21619: Popen objects no longer leave a zombie after exit in the with
statement if the pipe was broken. Patch by Martin Panter.
- Issue #6639: Module-level turtle functions no longer raise TclError after - Issue #6639: Module-level turtle functions no longer raise TclError after
closing the window. closing the window.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment