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

psutil.AccessDenied: psutil.AccessDenied (pid=1) #539

Closed
fanghuaqi opened this issue Nov 16, 2021 · 10 comments · Fixed by #546
Closed

psutil.AccessDenied: psutil.AccessDenied (pid=1) #539

fanghuaqi opened this issue Nov 16, 2021 · 10 comments · Fixed by #546
Labels
bug Something isn't working

Comments

@fanghuaqi
Copy link

Before opening an issue, please try starting terminator with an empty config
file, by issuing terminator -u -g /dev/null and see if it starts normally.

If it does, and you still want to open the bug, then please attach the
config file, usually found at $HOME/.config/terminator/config to the issue

Fill out as many of these fields as you can

Describe the bug
A clear and concise description of what the bug is.

Open built terminator by following INSTALL.md, and met this this issue:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/psutil/_common.py", line 449, in wrapper
    ret = self._cache[fun]
AttributeError: _cache

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/psutil/_pslinux.py", line 1515, in wrapper
    return fun(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/psutil/_common.py", line 452, in wrapper
    return fun(self)
  File "/usr/local/lib/python3.8/dist-packages/psutil/_pslinux.py", line 1557, in _parse_stat_file
    with open_binary("%s/%s/stat" % (self._procfs_path, self.pid)) as f:
  File "/usr/local/lib/python3.8/dist-packages/psutil/_common.py", line 713, in open_binary
    return open(fname, "rb", **kwargs)
PermissionError: [Errno 1] Operation not permitted: '/proc/1/stat'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/hqfang/mysofts/local/bin/terminator", line 65, in <module>
    ibus_running = [p for p in psutil.process_iter() if p.name() == 'ibus-daemon' and p.username() == username]
  File "/home/hqfang/mysofts/local/bin/terminator", line 65, in <listcomp>
    ibus_running = [p for p in psutil.process_iter() if p.name() == 'ibus-daemon' and p.username() == username]
  File "/usr/local/lib/python3.8/dist-packages/psutil/__init__.py", line 634, in name
    name = self._proc.name()
  File "/usr/local/lib/python3.8/dist-packages/psutil/_pslinux.py", line 1515, in wrapper
    return fun(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/psutil/_pslinux.py", line 1610, in name
    name = self._parse_stat_file()['name']
  File "/usr/local/lib/python3.8/dist-packages/psutil/_pslinux.py", line 1517, in wrapper
    raise AccessDenied(self.pid, self._name)
psutil.AccessDenied: psutil.AccessDenied (pid=1)

To Reproduce
Steps to reproduce the behavior:

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS or Linux Distribution: Ubuntu
  • Version 20.04.3 LTS
  • Display Technology: X11

Additional context
Add any other context about the problem here.

@fanghuaqi
Copy link
Author

The terminator was installed to my own folder, not system folder.

@mattrose
Copy link
Member

That looks like a problem in your installed version of psutil. What section of the INSTALL doc did you follow? The PPA install, or the Source install?

@mattrose mattrose added the question Further information is requested label Nov 16, 2021
@fanghuaqi
Copy link
Author

source install, yes, I have installed psutils myself.

@mattrose
Copy link
Member

Hmm... Can you run ps auxw | grep ibus-daemon and paste the results?

@fanghuaqi
Copy link
Author

fanghuaqi commented Nov 18, 2021

Here is the result:

ps auxw | grep ibus-daemon
hqfang   3548231  0.0  0.0   9516   744 pts/35   S+   12:44   0:00 grep --color=auto ibus-daemon

no ibus-daemon process found here.

@mattrose
Copy link
Member

OK, I still can't reproduce this. Can you try this?

cat <<EOF | python
import psutil
print(psutil.__version__)
EOF

and

cat << EOF | python
import psutil
username = "hqfang"
ibus_running = [p for p in psutil.process_iter() if p.name() == 'ibus-daemon' and p.username() == username]
print(ibus_running)

@fanghuaqi
Copy link
Author

Thanks for your such details steps, here is the output:

$ /usr/bin/python3
Python 3.8.10 (default, Sep 28 2021, 16:10:42) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> print(psutil.__version__)
5.7.2
>>> username = "hqfang"
>>> ibus_running = [p for p in psutil.process_iter() if p.name() == 'ibus-daemon' and p.username() == username]
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/psutil/_common.py", line 449, in wrapper
    ret = self._cache[fun]
AttributeError: _cache

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/psutil/_pslinux.py", line 1515, in wrapper
    return fun(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/psutil/_common.py", line 452, in wrapper
    return fun(self)
  File "/usr/local/lib/python3.8/dist-packages/psutil/_pslinux.py", line 1557, in _parse_stat_file
    with open_binary("%s/%s/stat" % (self._procfs_path, self.pid)) as f:
  File "/usr/local/lib/python3.8/dist-packages/psutil/_common.py", line 713, in open_binary
    return open(fname, "rb", **kwargs)
PermissionError: [Errno 1] Operation not permitted: '/proc/1/stat'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
  File "/usr/local/lib/python3.8/dist-packages/psutil/__init__.py", line 634, in name
    name = self._proc.name()
  File "/usr/local/lib/python3.8/dist-packages/psutil/_pslinux.py", line 1515, in wrapper
    return fun(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/psutil/_pslinux.py", line 1610, in name
    name = self._parse_stat_file()['name']
  File "/usr/local/lib/python3.8/dist-packages/psutil/_pslinux.py", line 1517, in wrapper
    raise AccessDenied(self.pid, self._name)
psutil.AccessDenied: psutil.AccessDenied (pid=1)
>>> 

@mattrose
Copy link
Member

I still can't reproduce this at all, even though I have the exact same version of python and psutil.

Are you running it in some kind of container, like a flatpak, or snap, or docker or something?

@Jamesits
Copy link

Jamesits commented Nov 29, 2021

I know why this is happening and how to reproduce this issue, as I work at the IT dept of the same company where @fanghuaqi works. He works on a shared workstation where /proc is mounted with hidepid=1 to mitigate a certain security issue, so when psutil.process_iter() tried to iterate through all the valid PIDs, it will certainly receive an EPERM sooner or later. (And there are other settings, when applied, can achieve the same result.)

To reproduce this issue, first execute with root permission:

mount -o remount,rw,nosuid,nodev,noexec,relatime,hidepid=1 /proc

Then start terminator as any non-root account.

I guess it can be fixed by ignoring all EPERM errors during psutil.process_iter() since a user will always have permission to access its own processes, and here we only want to iterate through ones of our own user; but I'm not sure if there are other implications. FYI, I applied a temporary patch on our workstation to keep our workflow running:

diff terminator.bak /usr/bin/terminator
61,62c61,68
<     ibus_running = [p for p in psutil.process_iter() if p.name() == 'ibus-daemon' and p.username() == username]
<     ibus_running = len(ibus_running) > 0
---
>     ibus_running = False
>     for p in psutil.process_iter():
>          try:
>              if p.name() == 'ibus-daemon' and p.username() == username:
>                  ibus_running = True
>                  break
>          except:
>              pass

You might want to only except a smaller scope of exceptions here.

Our system information, if relevant:

  • OS: Ubuntu 20.04.3 LTS
  • Kernel: 5.4.0-80-generic amd64
  • Terminator version: 2.1.1-2 from Ubuntu focal/main

@mattrose
Copy link
Member

Wow, that's some lockdown. I'll work that into a PR. That code looks good, but needs more to catch exceptions more specifically, as you mentioned. Thanks for helping @fanghuaqi and I out with this.

@mattrose mattrose reopened this Nov 29, 2021
@mattrose mattrose added bug Something isn't working and removed question Further information is requested labels Dec 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants