Search code examples
pythonpython-3.xpsutil

Python program with psutil fails after 8 hours of running with PermissionError: [WinError 5] Access is denied - why?


I have a Python 3.x program running in Windows that uses psutil. The job of this program is simple - to periodically check if a different program is running. If it finds the program, it does nothing. If it doesn't find the program, it starts the program. Then the program sleeps and does the same thing again in a loop. Here's a minimal version of the code:

import psutil, os, time

found = 0

theprogram = 'hiimaprogram.exe'

while True:

    for process in psutil.process_iter():
        if process.name() == theprogram:                   # this is line 14
            found = 1
            print ('process found, doing nothing')
            

    if found == 0:
        print ('process not found, starting the process')
        filepath = theprogram
        os.startfile(filepath)
        
    found = 0

    time.sleep(10)
    print ('sleeping')
    continue

The code works great and does the job nicely. However, it also fails randomly, never straight away, usually at the 8 hour mark although sometimes it'll survive just a little longer. Here's the traceback:

Traceback (most recent call last):
  File "site-packages\psutil\_pswindows.py", line 707, in wrapper
  File "site-packages\psutil\_pswindows.py", line 791, in exe
PermissionError: [WinError 5] Access is denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "site-packages\psutil\_pswindows.py", line 782, in name
  File "site-packages\psutil\_pswindows.py", line 709, in wrapper
psutil.AccessDenied: psutil.AccessDenied (pid=31216)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "site-packages\psutil\_pswindows.py", line 707, in wrapper
  File "site-packages\psutil\_pswindows.py", line 784, in name
ProcessLookupError: [Errno 3] No such process

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "programchecker.py", line 14, in <module>
  File "site-packages\psutil\__init__.py", line 731, in name
  File "site-packages\psutil\_pswindows.py", line 709, in wrapper
psutil.NoSuchProcess: psutil.NoSuchProcess process no longer exists (pid=31216)

Line 14 where the process dies is marked in the code. Easy enough for me to write a try/except to ignore the error, but I'd really like to know why it is happening. Try/except code:

while True:

    for process in psutil.process_iter():
        try:
            if process.name() == theprogram:                  # this is line 14
                found = 1
                print ('process found')
        except:
            print ('error')
            time.sleep(100)
            continue

Reading up on psutil and other people who have had the same error (but for different reasons) all I can find that seems to fit my situation is that I need to be in administrator mode, but running the program in administrator mode appears to make no difference at all and still shows the same error. The other thing I thought of is perhaps some automatic windows process is interfering at the eight hour mark due to sleep/hibernation but I've made sure that the computer is on an "always on" power scheme where nothing should be interfering (at least in theory). Any help appreciated, thank you!


Solution

  • I found the solution to this, which is completely ludicrous, but here it is.

    The actual program that my program is checking for the existence of, has a "security feature" built into it where it force-times itself out after the 8 hour mark, even if the program is active and being used. This is direct from the devs of that program. Insane, I know. Adding the try/except above fixes the issue, the sleep timeout of 100 is sufficient window for the master program to stop caring and reset the clock.