Add `signal.get_wakeup_fd()`
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Feature or enhancement
Proposal:
We have signal.set_wakeup_fd(), which sets a new file descriptor and returns the old one. However, there are at least three cases where we want to get the current wakeup fd without setting a new one:
- Obtaining debug information about the current state of the process, which also applies to tests (how can we ensure that a particular function actually sets the wakeup fd?).
- Raising warnings if it has been changed (reset to -1?) in some scope we are considering.
- Conditionally setting the wakeup fd depending on whether it has been changed in another scope.
The reason I opened this issue relates to the third case. I have an "inner" wakeup fd (guest) and an "outer" wakeup fd (host). When the outer wakeup fd is either not set (equal to -1) or equal to the inner wakeup fd, it is updated along with the inner one. In this case:
- If I do not set/get wakeup fd, I will not know the current inner wakeup fd, and as a result, I will not be able to perform checks.
- If I always set the outer wakeup fd when exiting the inner scope, then in case of equality, this may happen when the wakeup fd has already been closed (and reset to -1) in the inner scope, which in turn will lead to either an
OSErroror a possible write to a false file descriptor (since file descriptors are reusable). - I also cannot set the inner wakeup fd, since it could have been updated in the inner scope.
- The code in the inner scope is not under my control.
The simplest implementation of signal.get_wakeup_fd() at the Python level looks like this:
import signal
def get_wakeup_fd():
fd = signal.set_wakeup_fd(-1)
signal.set_wakeup_fd(fd)
return fd
However, it has two drawbacks:
- We lose the current value of the
warn_on_full_bufferparameter. - There is a race condition between the two
signal.set_wakeup_fd()calls (what if a signal is sent to the process during this time?), which can lead to lost signals (and thus the guest not waking up).
To solve the race condition problem, we would have to create our own wakeup pair, for example in the form of a non-blocking pipe, and send signals from the pipe to the current wakeup fd, effectively emulating the trip_signal() function. But this only sounds simple on Unix. On Windows:
-
It is not possible to create a non-blocking pipe using the public API without
ctypesuntil Python 3.12. The closest alternative using the private API would look like this:from _winapi import SetNamedPipeHandleState from msvcrt import get_osfhandle def set_blocking(pipefd, blocking, /): SetNamedPipeHandleState( get_osfhandle(pipefd), 0 if blocking else 1, None, None, ) -
We cannot use
os.write()if the wakeup fd is a socket. In this case, we would either have to try to handle sockets viasocket.fromfd(), or use an alternative approach withsignal.signal()+signal.raise_signal()(disable the current signal handlers, implicitly calltrip_signal()by resending signals to the current process, enable them back), but the latter is even more non-trivial and is unlikely to have a correct solution.
Meanwhile, at the signalmodule.c level, to get the current wakeup fd, it is enough to just get wakeup.fd. So why not add the corresponding function?
import signal
fd = signal.get_wakeup_fd()
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
- gh-145726
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 Modules/signalmodule.c 开始,重点查看 wakeup.fd 状态以及 issue 中链接的 trip_signal() 路径。检查现有的 signal.set_wakeup_fd() 行为及其文档,然后在开始之前检查链接的 PR gh-145726。完成的标准是:无需受到此处所述的 race 或 warn_on_full_buffer 限制即可获取当前的 wakeup fd,并且相关平台上的行为都得到覆盖。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- c, python
- 领域
- operating-systems
- Issue 类型
- 功能
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100