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.
Closing as per my previous #2400 (comment).
from psutil.
Related Issues (20)
- process_iter(): no longer check whether PIDs have been reused HOT 1
- Don't build with limited API for 3.13 free-threaded build
- [macOS] active system memory is inaccurate HOT 3
- Rename Process.connections() to Process.net_connections()
- Remove deprecated `Process.memory_info_ex()`
- [macOS] psutil.Process(pid).cpu_percent calculation result is inaccurate HOT 8
- [macOS] Lacking defines in <sys/mount.h> breaks build on legacy OS X HOT 1
- [Windows] Python 3.10 wheels are missing on PYPI for psutil 5.9.8
- blkio_ticks is not supported in solaris based system. (ret['blkio_ticks'] = fields[39] # aka 'delayacct_blkio_ticks')
- [Debian 12 bookworm] Repeating spikes in temperatures samples HOT 3
- About Error `FileNotFoundError: [Errno 2] No such file or directory: '/proc/12825/stat'` the some suggest HOT 11
- [Linux] cannot import name '_psutil_linux' from partially initialized module 'psutil' (most likely due to a circular import) HOT 3
- [mac os] thread id is not the native_id
- [All] Request: Make new Release and update PyPi HOT 1
- [Windows] win_service_get by PID and not by name HOT 3
- [macOS] build failure on OS X 10.4 HOT 1
- 'psutil' has no attribute 'sensors_temperatures'
- psutil fails (segfault) on Mac OS in the free-threaded (no GIL) version of Python 3.13 HOT 8
- [aarch64] IndexError on calling p.memory_maps()
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.