Skip to content
This repository has been archived by the owner on Jun 22, 2024. It is now read-only.

Commit

Permalink
OpenBSD: pid_exists() returns True for thread IDs (TIDs) (giampaolo#2395
Browse files Browse the repository at this point in the history
)
  • Loading branch information
giampaolo authored Apr 6, 2024
1 parent 5bac142 commit 841902c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

**Bug fixes**

- 2395_, [OpenBSD]: `pid_exists()`_ erroneously return True if the argument is
a thread ID (TID) instead of a PID (process ID).
- 2254_, [Linux]: offline cpus raise NotImplementedError in cpu_freq() (patch by Shade Gladden)
- 2272_: Add pickle support to psutil Exceptions.
- 2359_, [Windows], [CRITICAL]: `pid_exists()`_ disagrees with `Process`_ on
Expand Down
17 changes: 14 additions & 3 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,10 +561,9 @@ def pids():
return ret


if OPENBSD or NETBSD:
if NETBSD:

def pid_exists(pid):
"""Return True if pid exists."""
exists = _psposix.pid_exists(pid)
if not exists:
# We do this because _psposix.pid_exists() lies in case of
Expand All @@ -573,7 +572,19 @@ def pid_exists(pid):
else:
return True

else:
elif OPENBSD:

def pid_exists(pid):
exists = _psposix.pid_exists(pid)
if not exists:
return False
else:
# OpenBSD seems to be the only BSD platform where
# _psposix.pid_exists() returns True for thread IDs (tids),
# so we can't use it.
return pid in pids()

else: # FreeBSD
pid_exists = _psposix.pid_exists


Expand Down
11 changes: 8 additions & 3 deletions psutil/tests/test_process_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,12 @@ def check(pid):
if not WINDOWS: # see docstring
self.assertIn(pid, psutil.pids())
else:
with self.assertRaises(psutil.NoSuchProcess):
psutil.Process(pid)
# On OpenBSD thread IDs can be instantiated,
# and oneshot() succeeds, but other APIs fail
# with EINVAL.
if not OPENBSD:
with self.assertRaises(psutil.NoSuchProcess):
psutil.Process(pid)
if not WINDOWS: # see docstring
self.assertNotIn(pid, psutil.pids())
except (psutil.Error, AssertionError) as err:
Expand All @@ -531,7 +535,8 @@ def check(pid):
# Process class and is querable like a PID (process
# ID). Skip it.
continue
check(pid)
with self.subTest(pid=pid):
check(pid)


if __name__ == '__main__':
Expand Down

0 comments on commit 841902c

Please sign in to comment.