Comments (6)
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 kill()
system call that adds a process creation time as an argument as well as PID. Without that it won't help.
(I think the kernel developers would likely reject the idea stating it is practically not a problem.)
The pidfd
is probably not designed for the use case like ours, where we track the updates of all processes rather than a specific few. pidfd
would force the OS to reserve process references to us while a process manager app is not supposed to hold such resources for a long time. It's easy to mess up when managing the pidfd
resources.
from psutil.
Yes, the fact that pidfd_getfd()
can fail with "Too many open files" after 1024 calls basically kills this proposal. I believe pidfd_getfd()
is intended to be used by those apps which spawn and handle their own process children. It's not intended for htop-like apps which monitor all PIDs. Still, to me it looks like pidfd_getfd()
is also racy:
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 kill()
accepting a creation time arg: I'm not a kernel developer, and even if an OS were to support such a function, it wouldn't be cross platform.
I think the pid + create_time
pair strategy used by psutil is the best we can do. It's racy the same way pidfd_getfd()
is racy, but at least it can be implemented on all platforms. I expect the chances of incurring into the race condition to be very low, and to depend on the OS recycling algorithm. I would expect that the OS won't immediately reuse a PID, but who knows...
from psutil.
pid = os.fork() fd = pidfd_getfd(pid) pidfd_send_signal(fd)
If PID is reused between line 1 and 2 you have a race.
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.
Please understand the concept of "zombie processes" carefully.
from psutil.
Oh you're right! So pidfd_getfd()
is not racy in that case (but it is racy if PID is not a parent's child).
from psutil.
FreeBSD does not allow opening arbitrary processes for file descriptors. The "process descriptors" in FreeBSD can only be created by pdfork(2)
so process managers have no way to use them (they are surely designed for parent process only).
Linux has pidfd_open(2)
, which is what we can use.
from psutil.
Related Issues (20)
- [Windows 11] psutil.process_iter() is 10x slower when running from non-admin account than when running from ADMIN/elevated account HOT 5
- [macOS] CPU utilization contains zeroes HOT 7
- Windows build wheels failure HOT 1
- [CentOS 5] ethtool.h : error: unknown type name '__u32' HOT 1
- [CentOS Stream 10] test_linux.TestSystemCPUFrequency.test_emulate_multi_cpu fails on aarch64 HOT 3
- [CentOS Stream 10] test_misc.TestCommonModule.test_debug often fails on ppc64le, s390x, or aarch64 HOT 5
- [Enhancement] Refactoring of test HOT 1
- [Linux] psutil.tests.test_posix.TestProcess.test_nice fails under non-realtime scheduling policy HOT 2
- [OS] title Windows
- [macOS] cpu_freq() fails on arm64 HOT 6
- [Windows] as_dict() failing for win_service_get('WaaSMedicSvc')
- [Windows]Why are the indicators collected by psuntil too large? HOT 5
- [Linux] Distribute linux wheels for python versions > 3.6 on x86 and arm64 HOT 1
- [Linux] run "python3 -m psutil.tests", some test cases failed when nfs boot is used
- [Windows 10] psutil.process_iter() seems to be stuck
- [Linux] psutil.tests.test_linux.TestSystemVirtualMemoryAgainst tests failing
- [Linux] Get laptop charging speed (in Watts)
- process_iter(): no longer check whether PIDs have been reused HOT 1
- Don't build with limited API for 3.13 free-threaded build
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from psutil.