Comments (3)
I did some more testing. It looks like there's a lot more than pid 17320 where there's a mismatch:
>>> import psutil
>>> import psutil._psutil_windows
>>> pidinfo = []
>>> for i in range(30000):
... exists = psutil.pid_exists(i)
... try:
... proc = psutil.Process(i)
... except Exception as e:
... proc = e
... try:
... times = psutil._psutil_windows.proc_times(i)
... except Exception as e:
... times = e
... pidinfo.append((exists, proc, times))
>>> mismatches = [exists != isinstance(proc, psutil.Process) for exists, proc, times in pidinfo]
>>> mismatched_pids=[i for i, mismatched in enumerate(mismatches) if mismatched]
>>> mismatched_info=[(i, ) + pidinfo[i] for i in mismatched_pids]
>>> running_pids = [i for i, (exists, proc, times) in enumerate(pidinfo) if exists]
>>> len(running_pids)
2801
>>> len(mismatched_info)
1923
>>> running_pids[:10]
[0, 4, 5, 6, 7, 68, 69, 70, 71, 108]
>>> mismatched_info[:10]
[
(5, True, psutil.NoSuchProcess(pid=5, msg="process PID not found"), PermissionError(13, "Access is denied")),
(6, True, psutil.NoSuchProcess(pid=6, msg="process PID not found"), PermissionError(13, "Access is denied")),
(7, True, psutil.NoSuchProcess(pid=7, msg="process PID not found"), PermissionError(13, "Access is denied")),
(68, True, psutil.NoSuchProcess(pid=68, msg="process PID not found"), PermissionError(13, "Access is denied")),
(69, True, psutil.NoSuchProcess(pid=69, msg="process PID not found"), PermissionError(13, "Access is denied")),
(70, True, psutil.NoSuchProcess(pid=70, msg="process PID not found"), PermissionError(13, "Access is denied")),
(71, True, psutil.NoSuchProcess(pid=71, msg="process PID not found"), PermissionError(13, "Access is denied")),
(109, True, psutil.NoSuchProcess(pid=109, msg="process PID not found"), PermissionError(13, "Access is denied")),
(110, True, psutil.NoSuchProcess(pid=110, msg="process PID not found"), PermissionError(13, "Access is denied")),
(111, True, psutil.NoSuchProcess(pid=111, msg="process PID not found"), PermissionError(13, "Access is denied")),
]
>>> running_pids[-10:]
[28498, 28499, 28520, 28521, 28522, 28523, 28592, 28593, 28594, 28595]
>>> mismatched_info[-10:]
[
(28494, True, psutil.NoSuchProcess(pid=28494, msg="process PID not found"), PermissionError(13, "Access is denied")),
(28495, True, psutil.NoSuchProcess(pid=28495, msg="process PID not found"), PermissionError(13, "Access is denied")),
(28496, True, psutil.NoSuchProcess(pid=28496, msg="process PID not found"), PermissionError(13, "Access is denied")),
(28497, True, psutil.NoSuchProcess(pid=28497, msg="process PID not found"), PermissionError(13, "Access is denied")),
(28498, True, psutil.NoSuchProcess(pid=28498, msg="process PID not found"), PermissionError(13, "Access is denied")),
(28499, True, psutil.NoSuchProcess(pid=28499, msg="process PID not found"), PermissionError(13, "Access is denied")),
(28592, True, psutil.NoSuchProcess(pid=28592, msg="process PID not found"), PermissionError(13, "Access is denied")),
(28593, True, psutil.NoSuchProcess(pid=28593, msg="process PID not found"), PermissionError(13, "Access is denied")),
(28594, True, psutil.NoSuchProcess(pid=28594, msg="process PID not found"), PermissionError(13, "Access is denied")),
(28595, True, psutil.NoSuchProcess(pid=28595, msg="process PID not found"), PermissionError(13, "Access is denied")),
]
>>> pidinfo[17320]
(True, psutil.NoSuchProcess(pid=17320, msg="process PID not found"), PermissionError(13, "Access is denied"))
I checked the first 10 and last 10 mismatched pid's in task manager (I made sure to run it as Administrator). None were actually running. It seems that psutil.Process()
is right when it doesn't match psutil.pid_exists()
I know that 17320 is a process that used to be running, but no longer is. I wonder if that's true of the rest of these. I'm pretty sure 17320 was a python process in vscode debugger that I force-killed by stopping the debugger
from psutil.
Since this is a permission denied error, I thought I'd see what happens if I actually used multiple users (I ran these tests on windows 10, not 11)
on tapple account (administrator):
(psutil-venv) C:\Users\tapple>python
Python 3.11.0 (main, Oct 24 2022, 18:13:38) [MSC v.1933 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> p = psutil.Process()
>>> p
psutil.Process(pid=3592, name='python.exe', status='running', started='08:27:57')
on testuser account (not administrator):
(psutil-venv) C:\Users\testuser>python
Python 3.12.1 (tags/v3.12.1:2305ca5, Dec 7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> p = psutil.Process()
>>> p
psutil.Process(pid=13180, name='python3.12.exe', status='running', started='08:30:05')
>>> psutil.pid_exists(3592)
True
>>> other = psutil.Process(3592)
>>> other
psutil.Process(pid=3592, name='python.exe', status='running', started='08:27:57')
>>> other.username()
Traceback (most recent call last):
File "C:\Users\testuser\psutil-venv\Lib\site-packages\psutil\_pswindows.py", line 730, in wrapper
return fun(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\testuser\psutil-venv\Lib\site-packages\psutil\_pswindows.py", line 983, in username
domain, user = cext.proc_username(self.pid)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [WinError 5] Access is denied: '(originated from OpenProcess)'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\testuser\psutil-venv\Lib\site-packages\psutil\__init__.py", line 744, in username
return self._proc.username()
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\testuser\psutil-venv\Lib\site-packages\psutil\_pswindows.py", line 732, in wrapper
raise convert_oserror(err, pid=self.pid, name=self._name)
psutil.AccessDenied: (pid=3592, name='python.exe')
>>> other.kill()
Traceback (most recent call last):
File "C:\Users\testuser\psutil-venv\Lib\site-packages\psutil\_pswindows.py", line 730, in wrapper
return fun(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\testuser\psutil-venv\Lib\site-packages\psutil\_pswindows.py", line 917, in kill
return cext.proc_kill(self.pid)
^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [WinError 5] Access is denied: '(originated from OpenProcess)'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\testuser\psutil-venv\Lib\site-packages\psutil\__init__.py", line 1304, in kill
self._proc.kill()
File "C:\Users\testuser\psutil-venv\Lib\site-packages\psutil\_pswindows.py", line 732, in wrapper
raise convert_oserror(err, pid=self.pid, name=self._name)
psutil.AccessDenied: (pid=3592, name='python.exe')
back on tapple account (administrator):
>>> psutil.pid_exists(13180)
True
>>> other = psutil.Process(13180)
>>> other
psutil.Process(pid=13180, name='python3.12.exe', status='running', started='08:30:05')
>>> other.username()
Traceback (most recent call last):
File "C:\Users\tapple\psutil-venv\Lib\site-packages\psutil\_pswindows.py", line 730, in wrapper
return fun(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\tapple\psutil-venv\Lib\site-packages\psutil\_pswindows.py", line 983, in username
domain, user = cext.proc_username(self.pid)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [WinError 5] Access is denied: '(originated from OpenProcess)'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\tapple\psutil-venv\Lib\site-packages\psutil\__init__.py", line 744, in username
return self._proc.username()
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\tapple\psutil-venv\Lib\site-packages\psutil\_pswindows.py", line 732, in wrapper
raise convert_oserror(err, pid=self.pid, name=self._name)
psutil.AccessDenied: (pid=13180, name='python3.12.exe')
>>> other.kill()
Traceback (most recent call last):
File "C:\Users\tapple\psutil-venv\Lib\site-packages\psutil\_pswindows.py", line 730, in wrapper
return fun(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\tapple\psutil-venv\Lib\site-packages\psutil\_pswindows.py", line 917, in kill
return cext.proc_kill(self.pid)
^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [WinError 5] Access is denied: '(originated from OpenProcess)'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\tapple\psutil-venv\Lib\site-packages\psutil\__init__.py", line 1304, in kill
self._proc.kill()
File "C:\Users\tapple\psutil-venv\Lib\site-packages\psutil\_pswindows.py", line 732, in wrapper
raise convert_oserror(err, pid=self.pid, name=self._name)
psutil.AccessDenied: (pid=13180, name='python3.12.exe')
So, psutil is able to get basic info about a process of another user without permission denied (including time), but not more detailed info.
In conclusion I think the correct behavior is: If there is permission denied getting basic process info, the process really does not exist. So:
- psutil.Process() is correct
- psutil.pid_exists() is buggy
from psutil.
There's a similar bug for linux, #1910. In that bug, it's the opposite: pid_exists()
is correct and Process()
is incorrect. So, the only sure-fire way to check that a process exists, in spite of these two bugs is:
proc = None
try:
# check both pid_exists and Process. There are at least
# 2 issues about them disagreeing in different directions.
# See issues #1910 and #2359
if psutil.pid_exists(pid):
proc = psutil.Process(pid)
except (ProcessLookupError, psutil.NoSuchProcess):
pass
if proc is not None:
...
from psutil.
Related Issues (20)
- [macOS] error: use of undeclared identifier 'kIOMainPortDefault'
- [Linux] psutil.tests.test_linux.TestSystemVirtualMemoryAgainst tests failing
- [Linux] psutil.tests.test_linux.TestRootFsDeviceFinder.test_disk_partitions_mocked: IndexError: list index out of range
- [SUNOS] `Process.cmdline()` does not handle spaces properly HOT 3
- [SunOS] `saved=None` confuses `TestFetchAllProcesses.test_all`
- [macOS] v. 5.9.7 broken on macOS < 10.13: `if_dl.h: error: unknown type name 'u_char'; did you mean 'char'?` HOT 2
- [macOS] error: unknown type name 'CFTimeInterval' HOT 1
- Core usage anomaly
- [macOS] error: field has incomplete type 'struct sockaddr'
- [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
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.