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

About Error `FileNotFoundError: [Errno 2] No such file or directory: '/proc/12825/stat'` the some suggest #2418

Open IT-IvDragon opened 4 months ago

IT-IvDragon commented 4 months ago

when I was useing the function process_iter discover a not myself can catch the error。 For errors like FileNotFoundError: [Errno 2] No such file or directory: '/proc/xxx/stat'。 I searched for problems in lssues , but found that the latest version didn't fix them. Through the source code found that this function only catch the NoSuchProcess exception,and this error I think is a problem with pids functino,pids from /proc dir fetch a unusua path,lead to open the path /proc/xxx/stat raised an exception,so we can do try/catch the FileNotFoundError error at the open file location。

giampaolo commented 3 months ago

I cannot understand much from your report. Please paste your code and the full traceback message.

A1ines commented 2 months ago

I cannot understand much from your report. Please paste your code and the full traceback message.

I encountered the same problem. [Errno 2] No such file or directory: '/proc/1535931/stat' After investigating, I found that there are some anomalies with the /proc/1535931 directory. It doesn't contain the stat file, which causes the read error. image Since it runs on a server and I don't have permission to delete the anomalous process directory, I had to use a simple but effective method: skipping this PID. image Successfully run after modification.

IT-IvDragon commented 2 months ago

you can in the function process_iter add try finally ,skip the pid when happen error。

image

giampaolo commented 2 months ago

Are you saying file /proc/1535931/stat does not exist but directory /proc/1535931 exists? If that's the case, what's the content of directory /proc/1535931?

IT-IvDragon commented 2 months ago

yes ,I met the problem just not exist stat file, the other file/directory in directory /proc/1535931 is exist,I think is Linux system because of some special circumstances not create the stat Or the /proc/1535931 directory wasn't deleted

giampaolo commented 2 months ago

Can you do ls -la /proc/1535931 and paste the output?

IT-IvDragon commented 2 months ago

I tried ls ,but cannot find the progress id ,just the /proc/1535931 dir

giampaolo commented 2 months ago

So the /proc/1535931 directory is empty?

IT-IvDragon commented 2 months ago

not empty, there are some files, but no stat file

giampaolo commented 2 months ago

OK, paste those files. Paste the output of ls -la /proc/1535931 command.

IT-IvDragon commented 2 months ago

@A1ines