Skip to content

handle exception for psutil.NoSuchProcess along with psutil.AccessDenied #401

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yeggor
Copy link

@yeggor yeggor commented Apr 29, 2024

Hi. I've noticed that there can be a condition where process will be killed between

process = psutil.Process(pid)
and
mem = getattr(process, meminfo_attr)()[0] / _TWO_20

In this case, we get an exception psutil.NoSuchProcess: process no longer exists (pid=...), that we have to handle the same way as it's done here:

except (psutil.NoSuchProcess, psutil.AccessDenied):

@yeggor
Copy link
Author

yeggor commented Apr 29, 2024

here is trace from mprof run in case of triggered issue:

  File "/Users/user/pyenv/lib/python3.11/site-packages/mprof.py", line 283, in run_action
    mp.memory_usage(proc=p, interval=args.interval, timeout=args.timeout, timestamps=True,
  File "/Users/user/pyenv/lib/python3.11/site-packages/memory_profiler.py", line 405, in memory_usage
    mem_usage = _get_memory(
                ^^^^^^^^^^^^
  File "/Users/user/pyenv/lib/python3.11/site-packages/memory_profiler.py", line 221, in _get_memory
    return tools[backend]()
           ^^^^^^^^^^^^^^^^
  File "/Users/user/pyenv/lib/python3.11/site-packages/memory_profiler.py", line 140, in ps_util_tool
    mem = getattr(process, meminfo_attr)()[0] / _TWO_20
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/pyenv/lib/python3.11/site-packages/psutil/_common.py", line 486, in wrapper
    raise raise_from(err, None)
          ^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 3, in raise_from
  File "/Users/user/pyenv/lib/python3.11/site-packages/psutil/_common.py", line 484, in wrapper
    return fun(self)
           ^^^^^^^^^
  File "/Users/user/pyenv/lib/python3.11/site-packages/psutil/__init__.py", line 1084, in memory_info
    return self._proc.memory_info()
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/pyenv/lib/python3.11/site-packages/psutil/_psosx.py", line 346, in wrapper
    return fun(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/pyenv/lib/python3.11/site-packages/psutil/_psosx.py", line 444, in memory_info
    rawtuple = self._get_pidtaskinfo()
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/pyenv/lib/python3.11/site-packages/psutil/_psosx.py", line 351, in wrapper
    raise NoSuchProcess(self.pid, self._name)
psutil.NoSuchProcess: process no longer exists (pid=80279)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant