Skip to content

Commit

Permalink
fix #2418 / Linux: fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Nov 24, 2024
1 parent 0866e1e commit 13a336b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ XXXX-XX-XX

**Bug fixes**

- 2418_, [Linux]: fix race condition in case /proc/PID/stat does not exist, but
/proc/PID does, resulting in FileNotFoundError.
- 2470_, [Linux]: `users()`_ may return "localhost" instead of the actual IP
address of the user logged in.

Expand Down
7 changes: 6 additions & 1 deletion psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,12 @@ def wrapper(self, *args, **kwargs):
raise NoSuchProcess(self.pid, self._name)
except FileNotFoundError:
self._raise_if_zombie()
if not os.path.exists("%s/%s" % (self._procfs_path, self.pid)):
# /proc/PID directory may still exist, but the files within
# it may not, indicating the process is gone, see:
# https://github.com/giampaolo/psutil/issues/2418
if not os.path.exists(
"%s/%s/stat" % (self._procfs_path, self.pid)
):
raise NoSuchProcess(self.pid, self._name)
raise

Expand Down
9 changes: 9 additions & 0 deletions psutil/tests/test_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -2133,6 +2133,15 @@ def test_issue_1014(self):
p.memory_maps()
assert m.called

def test_issue_2418(self):
p = psutil.Process()
with mock_open_exception(
'/proc/%s/statm' % os.getpid(), FileNotFoundError
):
with mock.patch("os.path.exists", return_value=False):
with pytest.raises(psutil.NoSuchProcess):
p.memory_info()

@pytest.mark.skipif(not HAS_RLIMIT, reason="not supported")
def test_rlimit_zombie(self):
# Emulate a case where rlimit() raises ENOSYS, which may
Expand Down

0 comments on commit 13a336b

Please sign in to comment.