Mercurial > cpython-lru_cache-weakref
diff Lib/test/test_subprocess.py @ 80368:63ff4c9a2ed2 3.2
Fixes issue #16327: The subprocess module no longer leaks file descriptors
used for stdin/stdout/stderr pipes to the child when fork() fails.
| author | Gregory P. Smith <greg@krypto.org> |
|---|---|
| date | Sat, 10 Nov 2012 22:32:22 -0800 |
| parents | e938fa6be509 |
| children | 6ebfdfe74c04 a6a6c349af7e |
line wrap: on
line diff
--- a/Lib/test/test_subprocess.py Sat Nov 10 21:07:04 2012 -0800 +++ b/Lib/test/test_subprocess.py Sat Nov 10 22:32:22 2012 -0800 @@ -65,6 +65,18 @@ self.assertEqual(actual, expected, msg) +class PopenTestException(Exception): + pass + + +class PopenExecuteChildRaises(subprocess.Popen): + """Popen subclass for testing cleanup of subprocess.PIPE filehandles when + _execute_child fails. + """ + def _execute_child(self, *args, **kwargs): + raise PopenTestException("Forced Exception for Test") + + class ProcessTestCase(BaseTestCase): def test_call_seq(self): @@ -841,6 +853,27 @@ process.communicate() + # This test is Linux-ish specific for simplicity to at least have + # some coverage. It is not a platform specific bug. + @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()), + "Linux specific") + def test_failed_child_execute_fd_leak(self): + """Test for the fork() failure fd leak reported in issue16327.""" + fd_directory = '/proc/%d/fd' % os.getpid() + fds_before_popen = os.listdir(fd_directory) + with self.assertRaises(PopenTestException): + PopenExecuteChildRaises( + [sys.executable, '-c', 'pass'], stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + # NOTE: This test doesn't verify that the real _execute_child + # does not close the file descriptors itself on the way out + # during an exception. Code inspection has confirmed that. + + fds_after_exception = os.listdir(fd_directory) + self.assertEqual(fds_before_popen, fds_after_exception) + + # context manager class _SuppressCoreFiles(object): """Try to prevent core files from being created."""
