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

[Linux/Android] Handle case when no access to /proc/stats #1334

Closed
alxchk opened this issue Sep 11, 2018 · 11 comments
Closed

[Linux/Android] Handle case when no access to /proc/stats #1334

alxchk opened this issue Sep 11, 2018 · 11 comments

Comments

@alxchk
Copy link
Contributor

alxchk commented Sep 11, 2018

Android Oreo prohibited access to some info from proc. Likely it's worth to handle such cases without explicit exceptions.

Python2, Android Oreo, Termux

@giampaolo
Copy link
Owner

Unfortunately there is nothing that psutil can do about it. Raising AccessDenied is the right way to handle this situation.

@alxchk
Copy link
Contributor Author

alxchk commented Sep 11, 2018

But it's not raising AccessDenied. It's printing that exception using traceback in global exception handler. My suggestion is to silently ignore AccessDenied.

https://github.com/giampaolo/psutil/blob/master/psutil/_pslinux.py#L332

@giampaolo
Copy link
Owner

I find it hard to understand the traceback from my phone. Please paste the error as pure text.

@alxchk
Copy link
Contributor Author

alxchk commented Sep 11, 2018

>>> import psutil
Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/lib/python2.7/site-packages/psutil/_pslinux.py", line 329, in <module>
    set_scputimes_ntuple("/proc")
  File "/data/data/com.termux/files/usr/lib/python2.7/site-packages/psutil/_common.py", line 299, in wrapper
    ret = cache[key] = fun(*args, **kwargs)
  File "/data/data/com.termux/files/usr/lib/python2.7/site-packages/psutil/_pslinux.py", line 296, in set_scputimes_ntuple
    with open_binary('%s/stat' % procfs_path) as f:
  File "/data/data/com.termux/files/usr/lib/python2.7/site-packages/psutil/_pslinux.py", line 205, in open_binary
    return open(fname, "rb", **kwargs)
IOError: [Errno 13] Permission denied: '/proc/stat'
Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/lib/python2.7/site-packages/psutil/__init__.py", line 1608, in <module>
    _last_cpu_times = cpu_times()
  File "/data/data/com.termux/files/usr/lib/python2.7/site-packages/psutil/__init__.py", line 1602, in cpu_times
    return _psplatform.cpu_times()
  File "/data/data/com.termux/files/usr/lib/python2.7/site-packages/psutil/_pslinux.py", line 582, in cpu_times
    set_scputimes_ntuple(procfs_path)
  File "/data/data/com.termux/files/usr/lib/python2.7/site-packages/psutil/_common.py", line 299, in wrapper
    ret = cache[key] = fun(*args, **kwargs)
  File "/data/data/com.termux/files/usr/lib/python2.7/site-packages/psutil/_pslinux.py", line 296, in set_scputimes_ntuple
    with open_binary('%s/stat' % procfs_path) as f:
  File "/data/data/com.termux/files/usr/lib/python2.7/site-packages/psutil/_pslinux.py", line 205, in open_binary
    return open(fname, "rb", **kwargs)
IOError: [Errno 13] Permission denied: '/proc/stat'
Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/lib/python2.7/site-packages/psutil/__init__.py", line 1615, in <module>
    _last_per_cpu_times = cpu_times(percpu=True)
  File "/data/data/com.termux/files/usr/lib/python2.7/site-packages/psutil/__init__.py", line 1604, in cpu_times
    return _psplatform.per_cpu_times()
  File "/data/data/com.termux/files/usr/lib/python2.7/site-packages/psutil/_pslinux.py", line 595, in per_cpu_times
    set_scputimes_ntuple(procfs_path)
  File "/data/data/com.termux/files/usr/lib/python2.7/site-packages/psutil/_common.py", line 299, in wrapper
    ret = cache[key] = fun(*args, **kwargs)
  File "/data/data/com.termux/files/usr/lib/python2.7/site-packages/psutil/_pslinux.py", line 296, in set_scputimes_ntuple
    with open_binary('%s/stat' % procfs_path) as f:
  File "/data/data/com.termux/files/usr/lib/python2.7/site-packages/psutil/_pslinux.py", line 205, in open_binary
    return open(fname, "rb", **kwargs)
IOError: [Errno 13] Permission denied: '/proc/stat'
>>>

@giampaolo giampaolo reopened this Sep 11, 2018
@giampaolo
Copy link
Owner

I suppressed the message suppressed occurring at import time in 2e220c8. As such the exception will actually occur (and bubble up) on the first call to cpu_times() or cpu_percent() functions. Closing as fixed.

@z8512
Copy link

z8512 commented Apr 11, 2022

  File "/data/data/com.termux/files/usr/lib/python3.10/site-packages/psutil/_common.py", line 397, 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.10/site-packages/psutil/_pslinux.py", line 303, in <module>
    set_scputimes_ntuple("/proc")
  File "/data/data/com.termux/files/usr/lib/python3.10/site-packages/psutil/_common.py", line 399, in wrapper
    ret = cache[key] = fun(*args, **kwargs)
  File "/data/data/com.termux/files/usr/lib/python3.10/site-packages/psutil/_pslinux.py", line 270, in set_scputimes_ntuple
    with open_binary('%s/stat' % procfs_path) as f:
  File "/data/data/com.termux/files/usr/lib/python3.10/site-packages/psutil/_common.py", line 711, in open_binary
    return open(fname, "rb", **kwargs)
PermissionError: [Errno 13] Permission denied: '/proc/stat'```

But why?

@chapmanjacobd
Copy link

I also experience this often in my own program. I'm not sure which library I'm using is calling psutil but I also don't expect the many downstream library maintainers to each handle Android as a special case in every place that they use psutil

Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/lib/python3.10/site-packages/psutil/_common.py", line 398, 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.10/site-packages/psutil/_pslinux.py", line 285, in <module>
    set_scputimes_ntuple("/proc")
  File "/data/data/com.termux/files/usr/lib/python3.10/site-packages/psutil/_common.py", line 400, in wrapper
    ret = cache[key] = fun(*args, **kwargs)
  File "/data/data/com.termux/files/usr/lib/python3.10/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.10/site-packages/psutil/_common.py", line 727, in open_binary
    return open(fname, "rb", buffering=FILE_READ_BUFFER_SIZE)
PermissionError: [Errno 13] Permission denied: '/proc/stat'

@giampaolo
Copy link
Owner

"PermissionError: [Errno 13] Permission denied: '/proc/stat'"

Note that this happens on import psutil (which is bad) but don't crash the app: the exception is just printed.
Still, it's weird that Android doesn't allow reading something as basic as /proc/stat.
As previously said, there is likely nothing psutil can do about it.

@chapmanjacobd
Copy link

chapmanjacobd commented Sep 22, 2022

try:
    read_proc_stat
except PermissionError:
    is_android = any([k in os.environ for k in ['ANDROID_STORAGE']])
    if is_android:
        pass  # expected on this platform
    else:
        raise e

I don't know what process statistics are used for but it might make even more sense to do something like this:

try:
    value = read_proc_stat
except PermissionError as e:
    value = 'Permission Denied'
else:
    return value

@elmustafaa
Copy link

comment fieds witch contain raise

@phanirithvij
Copy link

phanirithvij commented May 26, 2023

"PermissionError: [Errno 13] Permission denied: '/proc/stat'"

Note that this happens on import psutil (which is bad) but don't crash the app: the exception is just printed. Still, it's weird that Android doesn't allow reading something as basic as /proc/stat. As previously said, there is likely nothing psutil can do about it.

@giampaolo so can the traceback be not printed for this too?

traceback.print_exc()

Or is this is too important to ignore on other platforms then detect if it is android and don't print traceback in android?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants