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

mac m1 RuntimeError: proc_pidinfo(PROC_PIDLISTFDS) 2/2 syscall failed #2116

Open
tinarooot opened this issue Jun 30, 2022 · 9 comments
Open

Comments

@tinarooot
Copy link

Summary

  • OS: mac m1
  • Architecture: arm
  • Psutil version: 5.9.1
  • Python version: 3.6.13
  • Type: core

Description

hello all:

my code:

def killPort(port):
    nc = psutil.net_connections()
    for n in nc:
        if n.laddr.port == port:
            each_pro = psutil.Process(n.pid)
            each_pro.terminate()
            each_pro.wait(timeout=3)

use error:

Traceback (most recent call last):
  File "/Users/tina/opt/anaconda3/envs/py_env_3/lib/python3.6/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/tina/opt/anaconda3/envs/py_env_3/lib/python3.6/site-packages/flask/app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/tina/opt/anaconda3/envs/py_env_3/lib/python3.6/site-packages/flask_cors/extension.py", line 165, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Users/tina/opt/anaconda3/envs/py_env_3/lib/python3.6/site-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/tina/opt/anaconda3/envs/py_env_3/lib/python3.6/site-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/Users/tina/PycharmProjects/genCode/generateTemplate/aiapp/views/base.py", line 131, in stop
    fileUtils.killPort(port)
  File "/Users/tina/PycharmProjects/genCode/generateTemplate/aiapp/utils/FileUtils.py", line 21, in killPort
    nc = psutil.net_connections()
  File "/Users/tina/opt/anaconda3/envs/py_env_3/lib/python3.6/site-packages/psutil/__init__.py", line 2158, in net_connections
    return _psplatform.net_connections(kind)
  File "/Users/tina/opt/anaconda3/envs/py_env_3/lib/python3.6/site-packages/psutil/_psosx.py", line 248, in net_connections
    cons = Process(pid).connections(kind)
  File "/Users/tina/opt/anaconda3/envs/py_env_3/lib/python3.6/site-packages/psutil/_psosx.py", line 343, in wrapper
    return fun(self, *args, **kwargs)
  File "/Users/tina/opt/anaconda3/envs/py_env_3/lib/python3.6/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

How can I solve this problem, thanks

@tinarooot tinarooot added the bug label Jun 30, 2022
Repository owner deleted a comment from tinarooot Sep 19, 2022
@giampaolo
Copy link
Owner

I could reproduce this on my virtualized osx and it turns out the crash is due proc_pidinfo() being called for PID 0.
@tinarooot please confirm this is fixed once you're back from vacation. :)

@horta
Copy link

horta commented Feb 27, 2023

I'm getting something very similar on my mac m1:

~/c/h3daemon main• ❱ cat is_listening_exception2.txt 
Traceback (most recent call last):
  File "/Users/horta/code/h3daemon/h3daemon/app.py", line 27, in is_listening
    for x in psutil.Process(pid).connections(kind="tcp"):
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/horta/Library/Caches/pypoetry/virtualenvs/h3daemon-HhWKenaR-py3.11/lib/python3.11/site-packages/psutil/__init__.py", line 1166, in connections
    return self._proc.connections(kind)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/horta/Library/Caches/pypoetry/virtualenvs/h3daemon-HhWKenaR-py3.11/lib/python3.11/site-packages/psutil/_psosx.py", line 346, in wrapper
    return fun(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/horta/Library/Caches/pypoetry/virtualenvs/h3daemon-HhWKenaR-py3.11/lib/python3.11/site-packages/psutil/_psosx.py", line 503, in connections
    rawlist = cext.proc_connections(self.pid, families, types)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: proc_pidinfo(PROC_PIDLISTFDS) 2/2 syscall failed

I'm storing the exception in a file because the parent process (pid in that is from one of its children) is running as a daemon using python-daemon.

I'm trying to figure out a minimal example.

@horta
Copy link

horta commented Feb 27, 2023

Python script to listen to a given port:

#!/usr/bin/env python
import socket
import sys

HOST = "0.0.0.0"
PORT = int(sys.argv[1])

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    with conn:
        while True:
            data = conn.recv(1024)
            if data.decode() == "bye":
                break
            conn.sendall(data)
            conn, addr = s.accept()

Script that will cause the exception given above if echo_cmd is used instead of nc_cmd:

import os
import traceback
from subprocess import Popen

import psutil
from daemon import DaemonContext

cwd = os.getcwd()

port = 51371
nc_cmd = ["/usr/bin/nc", "-l", str(port)]
echo_cmd = ["/Users/horta/code/h3daemon-bug/echo.py", str(port)]

if __name__ == "__main__":
    with DaemonContext():
        master = Popen(nc_cmd)
        # master = Popen(echo_cmd)

        x = master.pid
        try:
            for x in psutil.Process(master.pid).connections(kind="tcp"):
                pass
        except Exception:
            traceback.print_exc(file=open(f"{cwd}/exception.txt", "w"))

The bug does not happen without DaemonContext(), python-daemon.

lsof of nc:

nc        74113 horta  cwd       DIR               1,13          960            16841604 /Users/horta/code/h3daemon-bug
nc        74113 horta  txt       REG               1,13       203632 1152921500312423355 /usr/bin/nc
nc        74113 horta    0u     IPv4 0x88f18041015444b3          0t0                 TCP *:51371 (LISTEN)

lsof for echo.py:

python3.1 79307 horta  cwd       DIR               1,13         1024            16841604 /Users/horta/code/h3daemon-bug
python3.1 79307 horta  txt       REG               1,13      5408215             3456617 /Users/horta/.pyenv/versions/3.11.1/bin/python3.11
python3.1 79307 horta  txt       REG               1,13       101511             3467071 /Users/horta/.pyenv/versions/3.11.1/lib/python3.11/lib-dynload/math.cpython-311-darwin.so
python3.1 79307 horta  txt       REG               1,13       110624             1728429 /opt/homebrew/Cellar/gettext/0.21.1/lib/libintl.8.dylib
python3.1 79307 horta  txt       REG               1,13       122602             3467107 /Users/horta/.pyenv/versions/3.11.1/lib/python3.11/lib-dynload/_socket.cpython-311-darwin.so
python3.1 79307 horta  txt       REG               1,13        77033             3467093 /Users/horta/.pyenv/versions/3.11.1/lib/python3.11/lib-dynload/select.cpython-311-darwin.so
python3.1 79307 horta  txt       REG               1,13        89288             3467101 /Users/horta/.pyenv/versions/3.11.1/lib/python3.11/lib-dynload/array.cpython-311-darwin.so
python3.1 79307 horta    0u     IPv4 0x88f180410152c4b3          0t0                 TCP *:51371 (LISTEN)

@horta
Copy link

horta commented Dec 5, 2024

I'm still getting the same error on MacOS Sequoia, mac mini M1, psutil 6.1.0:

    connections = self._proc.net_connections(kind="tcp")
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/horta/code/hmmer3/h3daemon/.venv/lib/python3.12/site-packages/psutil/__init__.py", line 1244, in net_connections
    return self._proc.net_connections(kind)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/horta/code/hmmer3/h3daemon/.venv/lib/python3.12/site-packages/psutil/_psosx.py", line 349, in wrapper
    return fun(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/horta/code/hmmer3/h3daemon/.venv/lib/python3.12/site-packages/psutil/_psosx.py", line 511, in net_connections
    rawlist = cext.proc_net_connections(self.pid, families, types)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: proc_pidinfo(PROC_PIDLISTFDS) 2/2 syscall failed

@tinarooot

This comment was marked as off-topic.

@horta
Copy link

horta commented Dec 5, 2024

The solution might be here: https://github.com/lsof-org/lsof/blob/bbf320ce586a848f880bca7b758d50ae4c712624/lib/dialects/darwin/dproc.c#L480

It returns with no error in case errno = ESRCH, meaning "No process or process group can be found corresponding to that specified by pid."

@horta
Copy link

horta commented Dec 5, 2024

That would explain why I don't get that error when I provide stdout or stderr to the daemon process.

@horta
Copy link

horta commented Dec 5, 2024

My current workaround:

from psutil import Process

__all__ = ["tcp_connections"]


def tcp_connections(x: Process):
    # psutil bug: https://github.com/giampaolo/psutil/issues/2116
    with open("/dev/null", "wb"):
        connections = x.net_connections(kind="tcp")
    return connections

@giampaolo giampaolo reopened this Dec 5, 2024
@horta
Copy link

horta commented Dec 5, 2024

Sorry, the above workaround didnt work when I used breakpoints. Please, advise me on how to debug it for you...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants