New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Linux / FreeBSD] evaluate using pidfd_send_signal()
for signaling processes
#2400
Comments
pidfd_getfd()
+ pidfd_send_signal()
for signaling processespidfd_send_signal()
for signaling processes
Hi. I'm not sure if you are going to propose a solution for the kernel side. Since you mentioned about tracking the process creation time in htop-dev/htop#1441, I think a long term solution to the problem is to introduce a new The |
Yes, the fact that pid = os.fork()
fd = pidfd_getfd(pid)
pidfd_send_signal(fd) If PID is reused between line 1 and 2 you have a race. I've researched this topic for a while now, and the impression I got is that this problem is currently unsolvable. Basically as long as PIDs are represented as non-unique numbers, there's nothing a user space app can do. As for I think the |
Not if the process is the parent. In Unix-like systems, PIDs of dead children are reserved for parents and thus the PIDs won't be reused until the parent has done with them. |
Oh you're right! So |
FreeBSD does not allow opening arbitrary processes for file descriptors. The "process descriptors" in FreeBSD can only be created by Linux has |
See:
https://copyconstruct.medium.com/seamless-file-descriptor-transfer-between-processes-with-pidfd-and-pidfd-getfd-816afcd19ed4
Long story short:
pidfd_getfd()
used in conjunction withpidfd_send_signal()
would prevent signaling the wrong process PID in case PID is reused by another process. In theory this should not be a problem because psutil already pre-emptively checks if PID has been reused prior tokill()
,terminate()
,send_signal()
and all "set" methods (nice()
,ionice()
, etc.). Still, the pseudo code below (which represents what psutil does internally) is theoretically subject to a race condition in case PID gets reused between the execution of lines 1 and 2:pidfd_getfd()
+pidfd_send_signal()
would eliminate this theoretical risk at least forkill()
,terminate()
andsend_signal()
methods.It's interesting to note how FreeBSD offers similar syscalls, called
pdgetpid()
andpdkill()
:https://man.freebsd.org/cgi/man.cgi?query=procdesc&sektion=4
macOS doesn't seem to have them though.
Again, this is just a theoretical problem since we already check for PID reuse internally, so it's kind of low priority. Still, I wanted to write this down somewhere for future self-reference.
The text was updated successfully, but these errors were encountered: