pythonprofilers / memory_profiler

Monitor Memory usage of Python code
http://pypi.python.org/pypi/memory_profiler
Other
4.39k stars 380 forks source link

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

Open yeggor opened 6 months ago

yeggor commented 6 months ago

Hi. I've noticed that there can be a condition where process will be killed between https://github.com/pythonprofilers/memory_profiler/blob/025929f8e4f4ea8c27ddb5ef72fc91f6bd703ea5/memory_profiler.py#L134 and https://github.com/pythonprofilers/memory_profiler/blob/025929f8e4f4ea8c27ddb5ef72fc91f6bd703ea5/memory_profiler.py#L140

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: https://github.com/pythonprofilers/memory_profiler/blob/025929f8e4f4ea8c27ddb5ef72fc91f6bd703ea5/memory_profiler.py#L112

yeggor commented 6 months ago

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)