giampaolo / psutil

Cross-platform lib for process and system monitoring in Python
BSD 3-Clause "New" or "Revised" License
10.22k stars 1.38k forks source link

[MacOS] calling psutil.net_connections leads to deadlock of syncthing, tailscaled and qbittorrent processes. #2353

Open fff7d1bc opened 8 months ago

fff7d1bc commented 8 months ago

Summary

Description

I wrote myself a simple script that lists open ports, and noticed that a solid 7 out of 10 times I run it, it will lead to deadlocking both qbittorrent, tailscaled and syncthing, those will stay with 200-400% CPU usage and require SIGKILL to get rid of them. I can run lsof sudo lsof -i -P -n and it does not seems to trigger the probelm. Linux seems unaffected, must be something MacOS API specific.

#!/usr/bin/env python3

import psutil

for entry in psutil.net_connections(kind='inet'):
    if entry.status == 'LISTEN':
        proc = psutil.Process(entry.pid).as_dict()
        username = proc['username']
        name = proc['name']
        cmdline = proc['cmdline']
        cmdline = list(filter(None, cmdline))

        bind_ip = entry.laddr.ip
        bind_port = entry.laddr.port

        print(
            f"{username} {name} {bind_ip}:{bind_port}"
        )
fff7d1bc commented 8 months ago

After some more testing I think it might be related to the as_dict() method that lookup the individual connections of each process and this is how it triggers.