From f22548aa37d8b92fe1b1f5b08047eae7ba65754b Mon Sep 17 00:00:00 2001 From: Charles-Fran?ois Natali Date: Sun, 25 Aug 2013 18:24:45 +0200 Subject: Issue #18763: subprocess: The file descriptors are now closed after calling the preexec_fn callback, which may open file descriptors. --- Lib/test/test_subprocess.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'Lib/test/test_subprocess.py') diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 0aa4d21e31..f8cd1de325 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -1938,6 +1938,23 @@ class POSIXProcessTestCase(BaseTestCase): self.assertRaises(OSError, os.waitpid, pid, 0) self.assertNotIn(ident, [id(o) for o in subprocess._active]) + def test_close_fds_after_preexec(self): + fd_status = support.findfile("fd_status.py", subdir="subprocessdata") + + # this FD is used as dup2() target by preexec_fn, and should be closed + # in the child process + fd = os.dup(1) + self.addCleanup(os.close, fd) + + p = subprocess.Popen([sys.executable, fd_status], + stdout=subprocess.PIPE, close_fds=True, + preexec_fn=lambda: os.dup2(1, fd)) + output, ignored = p.communicate() + + remaining_fds = set(map(int, output.split(b','))) + + self.assertNotIn(fd, remaining_fds) + @unittest.skipUnless(mswindows, "Windows specific tests") class Win32ProcessTestCase(BaseTestCase): -- cgit v1.2.1