changeset: 94782:b5e9ddbdd4a7 branch: 3.4 parent: 94778:b7945565e150 user: Serhiy Storchaka date: Sat Feb 28 12:43:08 2015 +0200 files: Lib/subprocess.py Lib/test/test_subprocess.py Misc/NEWS description: Issue #21619: Popen objects no longer leave a zombie after exit in the with statement if the pipe was broken. Patch by Martin Panter. diff -r b7945565e150 -r b5e9ddbdd4a7 Lib/subprocess.py --- a/Lib/subprocess.py Fri Feb 27 15:10:03 2015 -0500 +++ b/Lib/subprocess.py Sat Feb 28 12:43:08 2015 +0200 @@ -896,10 +896,12 @@ self.stdout.close() if self.stderr: self.stderr.close() - if self.stdin: - self.stdin.close() - # Wait for the process to terminate, to avoid zombies. - self.wait() + try: # Flushing a BufferedWriter may raise an error + if self.stdin: + self.stdin.close() + finally: + # Wait for the process to terminate, to avoid zombies. + self.wait() def __del__(self, _maxsize=sys.maxsize): if not self._child_created: diff -r b7945565e150 -r b5e9ddbdd4a7 Lib/test/test_subprocess.py --- a/Lib/test/test_subprocess.py Fri Feb 27 15:10:03 2015 -0500 +++ b/Lib/test/test_subprocess.py Sat Feb 28 12:43:08 2015 +0200 @@ -2521,6 +2521,21 @@ stderr=subprocess.PIPE) as proc: 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(): unit_tests = (ProcessTestCase, diff -r b7945565e150 -r b5e9ddbdd4a7 Misc/NEWS --- a/Misc/NEWS Fri Feb 27 15:10:03 2015 -0500 +++ b/Misc/NEWS Sat Feb 28 12:43:08 2015 +0200 @@ -13,6 +13,9 @@ 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 closing the window.