# HG changeset patch # Parent cdc3837cb1fcfc4273b46e84c533ec7e91246b71 Issue 21619: Call wait() even on broken pipe error diff -r cdc3837cb1fc Lib/subprocess.py --- a/Lib/subprocess.py Tue Dec 16 03:21:54 2014 -0500 +++ b/Lib/subprocess.py Tue Dec 16 10:39:24 2014 +0000 @@ -900,10 +900,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 cdc3837cb1fc Lib/test/test_subprocess.py --- a/Lib/test/test_subprocess.py Tue Dec 16 03:21:54 2014 -0500 +++ b/Lib/test/test_subprocess.py Tue Dec 16 10:39:24 2014 +0000 @@ -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,