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

[Termux] PermissionError: [Errno 13] Permission denied: '/proc/stat' #2195

Closed
babaric-dev opened this issue Jan 23, 2023 · 11 comments
Closed
Labels

Comments

@babaric-dev
Copy link

babaric-dev commented Jan 23, 2023

Summary

  • OS: Android 10
  • Architecture: aarch64
  • Psutil version: 5.9.4
  • Python version: 3.11.1
  • Type: core

Description

>>> import psutil
Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/psutil/_common.py", line 399, in wrapper
    return cache[key]
           ~~~~~^^^^^
KeyError: (('/proc',), frozenset())
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/psutil/_pslinux.py", line 285, in <module>
    set_scputimes_ntuple("/proc")
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/psutil/_common.py", line 401, in wrapper
    ret = cache[key] = fun(*args, **kwargs)                                       ^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/psutil/_pslinux.py", line 268, in set_scputimes_ntuple
    with open_binary('%s/stat' % procfs_path) as f:                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/psutil/_common.py", line 728, in open_binary
    return open(fname, "rb", buffering=FILE_READ_BUFFER_SIZE)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: '/proc/stat'
>>>

From the error message above, 2 errors occured:

  1. KeyError (IDK why this appears)
  2. PermissionError (because of SeLinux restrictions on Android)

I want to focus on more about the second error. Because we never know which files are readable and which are not, we should handle exceptions raised by open(). We should add an try-except clause catching PermissionError. Another solution that I propose is to check whether the file is readable first before actually reading it. Both have their pros and cons, but I prefer the latter solution.

@giampaolo
Copy link
Owner

giampaolo commented Jan 23, 2023

Please paste the full error here instead of opening two tickets.

@giampaolo
Copy link
Owner

The error originates from here and occurs at import time. It's just printed, meaning it does not crash the app.

KeyError before PermissionError is legitimate, although a bit confusing: it just means caching didn't happen yet. We may want to improve that though.

As for PermissionError, I don't think there's anything we can do. If a basic file like /proc/stat cannot be read, I imagine tools like htop or ps won't work as well (unless they have SUID permissions).

@giampaolo
Copy link
Owner

KeyError before PermissionError is legitimate, although a bit confusing: it just means caching didn't happen yet. We may want to improve that though.

This part is now solved by #2196. The KeyError part is no longer shown in traceback messages.

@babaric-dev
Copy link
Author

A bit off-topic, but when will 5.9.5 be released?

@thewhiteh4t
Copy link

@giampaolo in version 5.9.5 specifically in termux, we cannot catch PermissionError ?

@thewhiteh4t
Copy link

Screenshot_20230822-233034.png

@Ammar-Husain
Copy link

So How to get rid of this annoying error each time i run the code ?

@romanovj
Copy link

romanovj commented Sep 11, 2023

@Ammar-Husain you can copy /proc/stat with adb and then read it (you will need to setup adb)

/data/data/com.termux/files/usr/lib/python3.11/site-packages/psutil/_pslinux.py

function def set_scputimes_ntuple(procfs_path):

replace

with open_binary('%s/stat' ...

with

os.system('adb shell cp /proc/stat /sdcard/stat')
with open_binary('%s/stat' % "/sdcard" ) as f:

or just copy file once

@purarue
Copy link

purarue commented Oct 13, 2023

Thought I would linkback to pipx issue discussing the traceback being printed

If psutil happens to be installed, since pipx uses userpath internally, which uses psutil in this function, every time I run pipx I see this traceback.

The actual code that uses psutil in userpath works fine on android:

>>> userpath.utils.get_parent_process_name()
'zsh'

would be nice to be able to silence the traceback without having to mess with adb or patching the psutil code, for now I've just been doing pipx ... 2>/dev/null but not the best solution

@giampaolo
Copy link
Owner

I have fixed this in becbe86. The error/exception is no longer printed on screen. It is only shown by using PSUTIL_DEBUG env var.

@thewhiteh4t
Copy link

works now, thanks a lot!!

ddelange added a commit to ddelange/psutil that referenced this issue Oct 16, 2023
* 'master' of https://github.com/giampaolo/psutil:
  pre-release
  give CREDIT to @JeremyGrosser and @getsentry for sponsorship (thanks\!)
  fix error in test_contracts.py on win
  chore: update GHA workflows (giampaolo#2315)
  fix giampaolo#2195 / linux: no longer print exception at import time
  Add ruff Python linter, remove flake8 (giampaolo#2312)
  Add toml-sort linting tool for pyproject.toml (giampaolo#2311)
  new RTD config
  new RTD config
  new RTD config
  Fix giampaolo#2308, OpenBSD: Process.threads() always fail with AccessDenied.
  Win: fix OpenProcess not recognizing when proc is gone.
  refact assertProcessGone
  refact assertProcessGone
  Add RsT linter (giampaolo#2292)
  Make _raise_if_pid_reused() raise NSP only if pid has been reused.
  fix DEVGUIDE.rst
  PID reusage is not checked for different set methods (giampaolo#2291)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants