Skip to content

Commit 1d89309

Browse files
committed
gh-139184: set O_CLOEXEC for master_fd when call os.forkpty
Signed-off-by: Manjusaka <[email protected]>
1 parent 3779f2b commit 1d89309

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

Lib/test/test_pty.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
raise unittest.SkipTest("pty is not available on this platform")
1313

1414
import errno
15+
import fcntl
1516
import os
1617
import pty
1718
import tty
@@ -230,6 +231,9 @@ def test_fork(self):
230231
os._exit(2)
231232
os._exit(4)
232233
else:
234+
flags = fcntl.fcntl(master_fd, fcntl.F_GETFD)
235+
cloexec_set = bool(flags & fcntl.FD_CLOEXEC)
236+
self.assertEqual(cloexec_set, True)
233237
debug("Waiting for child (%d) to finish." % pid)
234238
# In verbose mode, we have to consume the debug output from the
235239
# child or the child will block, causing this test to hang in the
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Set O_CLOEXEC for master_fd when call os.forkpty

Modules/posixmodule.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9059,14 +9059,22 @@ os_forkpty_impl(PyObject *module)
90599059
} else {
90609060
/* parent: release the import lock. */
90619061
PyOS_AfterFork_Parent();
9062+
/* set O_CLOEXEC on master_fd */
9063+
if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
9064+
goto error;
90629065
// After PyOS_AfterFork_Parent() starts the world to avoid deadlock.
90639066
if (warn_about_fork_with_threads("forkpty") < 0)
90649067
return NULL;
90659068
}
90669069
if (pid == -1) {
90679070
return posix_error();
90689071
}
9072+
90699073
return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
9074+
error:
9075+
if (master_fd != -1)
9076+
close(master_fd);
9077+
return NULL;
90709078
}
90719079
#endif /* HAVE_FORKPTY */
90729080

0 commit comments

Comments
 (0)