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

[OpenBSD], [NetBSD] Process cwd() can raise FileNotFoundError #2237

Closed
giampaolo opened this issue Apr 16, 2023 · 0 comments
Closed

[OpenBSD], [NetBSD] Process cwd() can raise FileNotFoundError #2237

giampaolo opened this issue Apr 16, 2023 · 0 comments

Comments

@giampaolo
Copy link
Owner

giampaolo commented Apr 16, 2023

Summary

  • OS: OpenBSD, NetBSD
  • Type: core

Description

Observed in unit tests:

======================================================================
ERROR: psutil.tests.test_contracts.TestFetchAllProcesses.test_all
----------------------------------------------------------------------
multiprocessing.pool.RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/vagrant/psutil/psutil/tests/test_contracts.py", line 389, in proc_info
    info[fun_name] = fun()
  File "/vagrant/psutil/psutil/__init__.py", line 719, in cwd
    return self._proc.cwd()
  File "/vagrant/psutil/psutil/_psbsd.py", line 566, in wrapper
    return fun(self, *args, **kwargs)
  File "/vagrant/psutil/psutil/_psbsd.py", line 842, in cwd
    return cext.proc_cwd(self.pid) or None
FileNotFoundError: [Errno 2] No such file or directory
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/vagrant/psutil/psutil/tests/test_contracts.py", line 431, in test_all
    for info in self.iter_proc_info():
  File "/usr/local/lib/python3.8/multiprocessing/pool.py", line 868, in next
    raise value
FileNotFoundError: [Errno 2] No such file or directory

Also reproducible with:

import psutil

for p in psutil.process_iter():
    print(p)
    try:
        print(p.cwd())
    except psutil.AccessDenied:
        print("AD")

...which prints:

psutil.Process(pid=63428, name='bash', status='sleeping', started='15:30:39')
Traceback (most recent call last):
  File "foo.py", line 6, in <module>
    print(p.cwd())
  File "/vagrant/psutil/psutil/__init__.py", line 719, in cwd
    return self._proc.cwd()
  File "/vagrant/psutil/psutil/_psbsd.py", line 566, in wrapper
    return fun(self, *args, **kwargs)
  File "/vagrant/psutil/psutil/_psbsd.py", line 842, in cwd
    return cext.proc_cwd(self.pid) or None
FileNotFoundError: [Errno 2] No such file or directory

Not sure why this happens. Probably the directory was deleted and internally the kernel calls stat() against it. Regardless, it should not crash with FileNotFoundError. Even though not documented, there are circumstances where we return None if cwd() cannot be determined (BSD*, AIX, SunOS), so we should do the same here.

EDIT: despite not directly cached in unit tests, the same problem can also occur on NetBSD.

@giampaolo giampaolo added the bug label Apr 16, 2023
@giampaolo giampaolo changed the title [OpenBSD] Process cwd() can raise FileNotFoundError [OpenBSD], [NetBSD] Process cwd() can raise FileNotFoundError Apr 16, 2023
ddelange added a commit to ddelange/psutil that referenced this issue Apr 21, 2023
* 'master' of https://github.com/giampaolo/psutil: (22 commits)
  use glob.glob() in setup.py
  OSX / refact: get rid of process_info.c (giampaolo#2243)
  OSX C refact: reconstruct _psutil_osx.c to preserve history
  OSX: rename psutil/_psutil_osx.c to arch/osx/proc.c to preserve GIT history
  OSX big C refactoring (giampaolo#2242)
  fix failing users() test; update HISTORY; give CREDITS to @0-wiz-0 for giampaolo#2241
  win C refact: reconstruct _psutil_windows.c trying to preserve history
  rename _psutil_windows.c -> proc.c
  Win, C, refact: move proc funcs into proc.c file
  BSD big refact: move proc funcs in new proc.c file
  Fix build on NetBSD due to missing .h include. (giampaolo#2241)
  Win, C, refact: move boot_time() and users() in new sys.c
  Windows / refact: new sensors.c file
  C refact: remove useless cmdline / cwd / environ layers. Call direct functions
  pre release
  Add CI testing for OpenBSD and NetBSD (giampaolo#2240)
  Fix giampaolo#2239 / proc name(): don't fail with ZombieProcess on cmdline()
  giampaolo#2238: passed wrong value to Py_BuildValue
  fix giampaolo#2238 if cwd() cannot be determined always return "" instead of None
  Fix giampaolo#2237, OpenBSD, cwd(): return None instead of FileNotFoundError
  ...
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

1 participant