os.forkpty() and pty.fork() return FD with O_CLOEXEC
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
(pid, fd) = pty.fork() # os.forkpty()
print('os.get_inheritable(fd))
https://docs.python.org/3/library/os.html#os.forkpty
https://docs.python.org/3/library/pty.html#pty.fork
Say nothing about that.
Lib/pty.py:
def fork():
"""fork() -> (pid, master_fd)
Fork and make the child a session leader with a controlling terminal."""
try:
pid, fd = os.forkpty()
except (AttributeError, OSError):
pass
else:
if pid == CHILD:
try:
os.setsid()
except OSError:
# os.forkpty() already set us session leader
pass
return pid, fd
master_fd, slave_fd = openpty()
pid = os.fork()
if pid == CHILD:
os.close(master_fd)
os.login_tty(slave_fd)
else:
os.close(slave_fd)
# Parent and child process.
return pid, master_fd
Also uses openpty() which also does not set O_CLOEXEC
Meanwhile, there is a C function posix_openpt(int flags); which allows setting O_CLOEXEC.
Seems, fd-leak safe solution is to boilerplate all the actions by hand. i.e. same way as pty.fork() does, but also implement openpty() using posix_openpt() + ptsname() / ptsname_r().
Just setting O_CLOEXEC after openpty() or after calling C forkpty() is not enough because of race-condition and possible file descriptor leak.
So, CLOEXEC behavior should be documented and race-condition prevented
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-139408
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 Lib/pty.py 以及文档中记载的入口点 os.forkpty() 和 pty.fork() 开始,然后将它们的 openpty() 路径与报告中提到的 posix_openpt(int flags) 接口进行比较。当 CLOEXEC 行为得到文档说明,并且 fork/openpty 路径能够防止所述竞争条件导致的文件描述符泄漏时,工作即告完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- operating-systems
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100