From 249cdc39fcfb0405ccb2c952f5d8bfce77deb53d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Fran=C3=A7ois=20Natali?= 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. --- Modules/_posixsubprocess.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'Modules/_posixsubprocess.c') diff --git a/Modules/_posixsubprocess.c b/Modules/_posixsubprocess.c index 07d709688c..8d655306ff 100644 --- a/Modules/_posixsubprocess.c +++ b/Modules/_posixsubprocess.c @@ -412,17 +412,6 @@ child_exec(char *const exec_array[], POSIX_CALL(close(errwrite)); } - if (close_fds) { - int local_max_fd = max_fd; -#if defined(__NetBSD__) - local_max_fd = fcntl(0, F_MAXFD); - if (local_max_fd < 0) - local_max_fd = max_fd; -#endif - /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */ - _close_open_fd_range(3, local_max_fd, py_fds_to_keep); - } - if (cwd) POSIX_CALL(chdir(cwd)); @@ -451,6 +440,18 @@ child_exec(char *const exec_array[], /* Py_DECREF(result); - We're about to exec so why bother? */ } + /* close FDs after executing preexec_fn, which might open FDs */ + if (close_fds) { + int local_max_fd = max_fd; +#if defined(__NetBSD__) + local_max_fd = fcntl(0, F_MAXFD); + if (local_max_fd < 0) + local_max_fd = max_fd; +#endif + /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */ + _close_open_fd_range(3, local_max_fd, py_fds_to_keep); + } + /* This loop matches the Lib/os.py _execvpe()'s PATH search when */ /* given the executable_list generated by Lib/subprocess.py. */ saved_errno = 0; -- cgit v1.2.1