Skip to content

Commit

Permalink
OpenBSD: pid_exists() returns True for thread IDs (TIDs)
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Apr 6, 2024
1 parent 5bac142 commit d79c018
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
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 d79c018

Please sign in to comment.