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: pid_exists() returns True for thread IDs (TIDs) #2395

Merged
merged 2 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading