giampaolo / psutil

Cross-platform lib for process and system monitoring in Python
BSD 3-Clause "New" or "Revised" License
10.32k stars 1.39k forks source link

[Windows 10 22H2] psutil.process_iter() results in PermissionError: [WinError 5] Access denied: '(originated from OpenProcess)' #2294

Closed shinrax2 closed 2 weeks ago

shinrax2 commented 1 year ago

Summary

Description

Traceback (most recent call last):
  File "C:\Users\shinrax2\AppData\Roaming\Python\Python311\site-packages\psutil\_pswindows.py", line 797, in cmdline
    ret = cext.proc_cmdline(self.pid, use_peb=True)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [WinError 5] Zugriff verweigert: '(originated from OpenProcess)'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\shinrax2\AppData\Roaming\Python\Python311\site-packages\psutil\_pswindows.py", line 696, in wrapper
    return fun(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\shinrax2\AppData\Roaming\Python\Python311\site-packages\psutil\_pswindows.py", line 712, in wrapper
    return fun(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\shinrax2\AppData\Roaming\Python\Python311\site-packages\psutil\_pswindows.py", line 800, in cmdline
    ret = cext.proc_cmdline(self.pid, use_peb=False)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [WinError 5] Zugriff verweigert: '(originated from OpenProcess)'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\shinrax2\Documents\GitHub\PS3GameUpdateDownloader\dist\PS3GameUpdateDownloaderDebug\updater.py", line 23, in 
    utils.waitForMainAppExit()
  File "C:\Users\shinrax2\Documents\GitHub\PS3GameUpdateDownloader\dist\PS3GameUpdateDownloaderDebug\utils.py", line 385, in waitForMainAppExit
    if exename in p.name().lower() or exename in (' '.join(p.cmdline()).lower()):
                                                           ^^^^^^^^^^^
  File "C:\Users\shinrax2\AppData\Roaming\Python\Python311\site-packages\psutil\__init__.py", line 705, in cmdline
    return self._proc.cmdline()
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\shinrax2\AppData\Roaming\Python\Python311\site-packages\psutil\_pswindows.py", line 698, in wrapper
    raise convert_oserror(err, pid=self.pid, name=self._name)
psutil.AccessDenied: (pid=148, name='Registry')
Traceback
import psutil
def waitForMainAppExit():
    exename = (getExecutableBasename()+getExecutableSuffix()).lower()
    print(f"looking for {exename}")
    pid = None
    sleep_interval = 0.05
    for p in psutil.process_iter(attrs=["pid", "name"]):
        if exename in p.name().lower() or exename in (' '.join(p.cmdline()).lower()):
            pid = p.pid()
            print(f"found {exename} with pid: {pid}")
    if pid is not None:
        print(f"waiting for {exename} with interval of {sleep_interval}s")
        while psutil.pid_exists(pid) == True:
            time.sleep(sleep_interval)
utils.waitForMainAppExit() (reproduction code)

Additionally i tried running the code as administrator but that didn't change anything. If anymore info is required please @ me

giampaolo commented 2 weeks ago

AccessDenied is not a bug.