Is psutil.net_connections() unavailable for OSX?
Asked Answered
R

2

10

I was trying to run psutil.net_connections() on my OS X (12.1) Macbook Pro in python, but was greeted with the error of syscall failed. This is weird because most of ther other functions of psutil worked fine with no issues, yet somehow net_connections seemed to be the only one not working.

Python 3.8.9 (default, Oct 26 2021, 07:25:53) 
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> psutil.net_connections(kind='tcp')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mastermi/Library/Python/3.8/lib/python/site-packages/psutil/__init__.py", line 2161, in net_connections
    return _psplatform.net_connections(kind)
  File "/Users/mastermi/Library/Python/3.8/lib/python/site-packages/psutil/_psosx.py", line 248, in net_connections
    cons = Process(pid).connections(kind)
  File "/Users/mastermi/Library/Python/3.8/lib/python/site-packages/psutil/_psosx.py", line 343, in wrapper
    return fun(self, *args, **kwargs)
  File "/Users/mastermi/Library/Python/3.8/lib/python/site-packages/psutil/_psosx.py", line 500, in connections
    rawlist = cext.proc_connections(self.pid, families, types)
RuntimeError: proc_pidinfo(PROC_PIDLISTFDS) 2/2 syscall failed

If anyone know how to fix this issue it would be greatly appreciated.

P.S. My psutil is version 5.9.0 if that helps

Reiners answered 15/2, 2022 at 10:1 Comment(4)
@Jerey Chow any update on it? I have same issue for nowButterfat
Same for Python 3.9.1, psutil 5.9.1Hairy
The same code works on Ubuntu. Potentially a bug in macOS.Hairy
Python 3.10 and psutil 5.9.1 gives error that root privileges are required but if you then run as root you get proc_pidinfo(PROC_PIDLISTFDS) 2/2 syscall failedHolder
D
4

Short answer: Yes, it is unavailable.

Long answer: The most likely reason this isn't working on macOS is because it doesn't have a proc filesystem. It isn't much of a bug as it is a lacking feature. As an alternative, you could try sysctl which does have some of the features as proc, but I'm not sure if it fits your use case.

Demisemiquaver answered 8/7, 2022 at 16:17 Comment(0)
K
0

Workaround for this is to uninstall psutil and reinstall it

pip uninstall psutil

pip install --no-binary :all: psutil
Katinakatine answered 16/11, 2023 at 12:19 Comment(1)
If this is true, that implies that the published binaries (for some specific version or range of versions of psutil) are compiled against an older version of MacOS that isn't ABI-compatible with the latest one. An answer that's able to run down more details (exactly when the changed happen) would be valuable.Superheat

© 2022 - 2024 — McMap. All rights reserved.