psutil icon indicating copy to clipboard operation
psutil copied to clipboard

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

Open acongy opened this issue 3 years ago • 9 comments

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

acongy avatar Jun 30 '22 02:06 acongy

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. :)

giampaolo avatar Sep 19 '22 22:09 giampaolo

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 avatar Feb 27 '23 15:02 horta

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 avatar Feb 27 '23 16:02 horta

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

horta avatar Dec 05 '24 10:12 horta

这是来自QQ邮箱的假期自动回复邮件。你好,我最近正在休假中,无法亲自回复你的邮件。我将在假期结束后,尽快给你回复。

acongy avatar Dec 05 '24 10:12 acongy

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 avatar Dec 05 '24 12:12 horta

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

horta avatar Dec 05 '24 12:12 horta

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

horta avatar Dec 05 '24 12:12 horta

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

horta avatar Dec 05 '24 13:12 horta