psutil icon indicating copy to clipboard operation
psutil copied to clipboard

`Process.open_files()` raises IndexError in some linux environments

Open orionlee opened this issue 5 months ago • 1 comments

Process.open_files() raises IndexError in some linux environment at the following line.

https://github.com/giampaolo/psutil/blob/ea5b55605f857affa4e65fa27eb80f4f2bfebd63/psutil/_pslinux.py#L2256

Specifically, I got the error with Docker python:3.11-slim image running in Google Cloud Run.

The stacktrace:

Traceback (most recent call last):
  File "/app/trial/main.py", line 62, in get_psutil_result
    res = p.open_files()
          ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/psutil/__init__.py", line 1210, in open_files
    return self._proc.open_files()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/psutil/_pslinux.py", line 1646, in wrapper
    return fun(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/psutil/_pslinux.py", line 2256, in open_files
    flags = int(f.readline().split()[1], 8)
                ~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

orionlee avatar Jul 03 '25 01:07 orionlee

It appears that in this specific environment, the fdinfo file content is different from what psutil assumes.

I did some tracing. The fdinfo file in Docker + Google Cloud Run only has 1 single line (missing the expected line for pos):

flags:	02100000

In contrast, the fdinfo file in my local environment looks like this, compatible with what psutil assumes:

pos:	0
flags:	02100000
mnt_id:	129
ino:	29836347532685335

orionlee avatar Jul 03 '25 01:07 orionlee

hi @orionlee your PR does not work. Sometimes, pos is None, which leads to an error.

christopher5106 avatar Dec 14 '25 22:12 christopher5106