Skip to content

Commit ce34410

Browse files
authored
bpo-32270: Don't close stdin/out/err in pass_fds (GH-6242)
When subprocess.Popen() stdin= stdout= or stderr= handles are specified and appear in pass_fds=, don't close the original fds after dup'ing them. This implementation and unittest primarily came from @izbyshev (see the PR) See also izbyshev@b89b52f This also removes the old manual p2cread, c2pwrite, and errwrite closing logic as inheritable flags and _close_open_fds takes care of that properly today without special treatment. This code is within child_exec() where it is the only thread so there is no race condition between the dup and _Py_set_inheritable_async_safe call.
1 parent 880d42a commit ce34410

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

Lib/test/test_subprocess.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,6 +2529,36 @@ def test_pass_fds_inheritable(self):
25292529
self.assertEqual(os.get_inheritable(inheritable), True)
25302530
self.assertEqual(os.get_inheritable(non_inheritable), False)
25312531

2532+
2533+
# bpo-32270: Ensure that descriptors specified in pass_fds
2534+
# are inherited even if they are used in redirections.
2535+
# Contributed by @izbyshev.
2536+
def test_pass_fds_redirected(self):
2537+
"""Regression test for https://bugs.python.org/issue32270."""
2538+
fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
2539+
pass_fds = []
2540+
for _ in range(2):
2541+
fd = os.open(os.devnull, os.O_RDWR)
2542+
self.addCleanup(os.close, fd)
2543+
pass_fds.append(fd)
2544+
2545+
stdout_r, stdout_w = os.pipe()
2546+
self.addCleanup(os.close, stdout_r)
2547+
self.addCleanup(os.close, stdout_w)
2548+
pass_fds.insert(1, stdout_w)
2549+
2550+
with subprocess.Popen([sys.executable, fd_status],
2551+
stdin=pass_fds[0],
2552+
stdout=pass_fds[1],
2553+
stderr=pass_fds[2],
2554+
close_fds=True,
2555+
pass_fds=pass_fds):
2556+
output = os.read(stdout_r, 1024)
2557+
fds = {int(num) for num in output.split(b',')}
2558+
2559+
self.assertEqual(fds, {0, 1, 2} | frozenset(pass_fds), f"output={output!a}")
2560+
2561+
25322562
def test_stdout_stdin_are_single_inout_fd(self):
25332563
with io.open(os.devnull, "r+") as inout:
25342564
p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The subprocess module no longer mistakenly closes redirected fds even when
2+
they were in pass_fds when outside of the default {0, 1, 2} set.

Modules/_posixsubprocess.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,20 @@ child_exec(char *const exec_array[],
422422

423423
/* When duping fds, if there arises a situation where one of the fds is
424424
either 0, 1 or 2, it is possible that it is overwritten (#12607). */
425-
if (c2pwrite == 0)
425+
if (c2pwrite == 0) {
426426
POSIX_CALL(c2pwrite = dup(c2pwrite));
427-
while (errwrite == 0 || errwrite == 1)
427+
/* issue32270 */
428+
if (_Py_set_inheritable_async_safe(c2pwrite, 0, NULL) < 0) {
429+
goto error;
430+
}
431+
}
432+
while (errwrite == 0 || errwrite == 1) {
428433
POSIX_CALL(errwrite = dup(errwrite));
434+
/* issue32270 */
435+
if (_Py_set_inheritable_async_safe(errwrite, 0, NULL) < 0) {
436+
goto error;
437+
}
438+
}
429439

430440
/* Dup fds for child.
431441
dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
@@ -451,14 +461,8 @@ child_exec(char *const exec_array[],
451461
else if (errwrite != -1)
452462
POSIX_CALL(dup2(errwrite, 2)); /* stderr */
453463

454-
/* Close pipe fds. Make sure we don't close the same fd more than */
455-
/* once, or standard fds. */
456-
if (p2cread > 2)
457-
POSIX_CALL(close(p2cread));
458-
if (c2pwrite > 2 && c2pwrite != p2cread)
459-
POSIX_CALL(close(c2pwrite));
460-
if (errwrite != c2pwrite && errwrite != p2cread && errwrite > 2)
461-
POSIX_CALL(close(errwrite));
464+
/* We no longer manually close p2cread, c2pwrite, and errwrite here as
465+
* _close_open_fds takes care when it is not already non-inheritable. */
462466

463467
if (cwd)
464468
POSIX_CALL(chdir(cwd));

0 commit comments

Comments
 (0)