diff options
author | Gregory P. Smith <greg@krypto.org> | 2012-11-11 06:33:23 (GMT) |
---|---|---|
committer | Gregory P. Smith <greg@krypto.org> | 2012-11-11 06:33:23 (GMT) |
commit | 561cbc4e7bdf457bb64acf8ef7e5358795816c88 (patch) | |
tree | 6a9a8a16fdc9b5f69f1698226abff747cf47385a /Lib/test/test_subprocess.py | |
parent | a450c5e69ba17bb47e8b3434969218c97cdf1a07 (diff) | |
parent | 3d8e776cd932ff79fe181984cacce9ff8bcf3e4c (diff) | |
download | cpython-561cbc4e7bdf457bb64acf8ef7e5358795816c88.zip cpython-561cbc4e7bdf457bb64acf8ef7e5358795816c88.tar.gz cpython-561cbc4e7bdf457bb64acf8ef7e5358795816c88.tar.bz2 |
Fixes issue #16327: The subprocess module no longer leaks file descriptors
used for stdin/stdout/stderr pipes to the child when fork() fails.
Diffstat (limited to 'Lib/test/test_subprocess.py')
-rw-r--r-- | Lib/test/test_subprocess.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index b260c81..2d77554 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -68,6 +68,18 @@ class BaseTestCase(unittest.TestCase): 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): @@ -995,6 +1007,27 @@ class ProcessTestCase(BaseTestCase): 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.""" |