Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError 'psutil' in some __del__ call inside python shutdown #2437

Closed
yoori opened this issue Sep 1, 2024 · 1 comment
Closed

KeyError 'psutil' in some __del__ call inside python shutdown #2437

yoori opened this issue Sep 1, 2024 · 1 comment

Comments

@yoori
Copy link

yoori commented Sep 1, 2024

Summary

  • OS: linux, debian, ubuntu
  • Architecture: 64bit
  • Psutil version: 5.4.3
  • Python version: 3.6.8, 3.11.9
  • Type: core

Description

psutil, in general case, cannot be used in a some class __del__ method (directly or indirectly),
because __del__ can be called on python shutdown - and here no sys.modules['psutil'] record (it already removed):

import psutil
import os

class Test:
  def __del__(self):
    cur_process = psutil.Process(os.getpid())

t = Test()

Result :

Exception ignored in: <bound method Test.__del__ of <__main__.Test object at 0x7f1297886d68>>
Traceback (most recent call last):
  File "n.py", line 6, in __del__
  File "/usr/lib64/python3.6/site-packages/psutil/__init__.py", line 341, in __init__
  File "/usr/lib64/python3.6/site-packages/psutil/__init__.py", line 363, in _init
  File "/usr/lib64/python3.6/site-packages/psutil/_pslinux.py", line 1417, in __init__
  File "/usr/lib64/python3.6/site-packages/psutil/_pslinux.py", line 214, in get_procfs_path
KeyError: ('psutil',)

In my case I try to use psutil for kill frozen chromedriver child processes for exclude zombies formation - and this psutil behavior is strange for me ...

@yoori yoori added the bug label Sep 1, 2024
@github-actions github-actions bot added the linux label Sep 1, 2024
@giampaolo
Copy link
Owner

This behavior is related to how cPython shuts down, not to psutil per se. I think you would have a similar problem also with other imports. E.g. even if you move import psutil into the __del__ method you'll get this:

$ python3 foo.py 
Exception ignored in: <function Test.__del__ at 0x78e756911750>
Traceback (most recent call last):
  File "/home/giampaolo/foo.py", line 9, in __del__
ImportError: sys.meta_path is None, Python is likely shutting down

More in general, __del__ is tricky. AFAIK it is not even guaranteed to run, and as such, is not a good place for cleanup code. It's usually better to put cleanup code in a finally clause as in:

try:
    main()
finally:
    cleanup()

...or:

import atexit

@atexit.register
def cleanup():
   ...

Try one of the 2 approaches above and forget about __del__ (at least in this specific scenario).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants