Maselkov / GW2RPC

Discord Rich Presence addon for Guild Wars 2
https://gw2rpc.info
GNU General Public License v3.0
46 stars 12 forks source link

Fix race condition on process iteration #7

Closed Pluckerpluck closed 5 years ago

Pluckerpluck commented 5 years ago

So I ran this program at startup, and without ever running GW2 since boot the process crashed.

I believe it's due to the race condition created when running process.name(). There is a chance that the process no longer exists during the loop, and process.name() requires the process to exist to work. Therefore there is a slim chance of a crash here.

So this pull request uses the attributes parameter in the process_iter function in order to cache the name attribute ensuring this crash does not occur again.

The error I received is below:

2018-09-19 16:18:43,050:CRITICAL:root: GW2RPC has crashed
Traceback (most recent call last):
  File "site-packages\psutil\_pswindows.py", line 635, in wrapper
  File "site-packages\psutil\_pswindows.py", line 701, in exe
ProcessLookupError: [Errno 3] No such process

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "gw2rpc\gw2rpc.py", line 373, in main_loop
  File "gw2rpc\gw2rpc.py", line 353, in update_gw2_process
  File "site-packages\psutil\__init__.py", line 604, in name
  File "site-packages\psutil\_pswindows.py", line 635, in wrapper
  File "site-packages\psutil\_pswindows.py", line 687, in name
  File "site-packages\psutil\_pswindows.py", line 640, in wrapper
psutil._exceptions.NoSuchProcess: psutil.NoSuchProcess process no longer exists (pid=16932)
Maselkov commented 5 years ago

Thank you! Had some people reporting this issue but didn't really have time to look into it too much