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

Expose parameter to return partial results in case of EPERM / AccessDenied #2329

Open
giampaolo opened this issue Nov 8, 2023 · 0 comments

Comments

@giampaolo
Copy link
Owner

giampaolo commented Nov 8, 2023

Problem

psutil provides different APIs that return multiple results (a list or a dict of elements), like psutil.Process.open_files() or psutil.net_connections(). It can happen that these API raise AccessDenied because of insufficient permissions, despite the fact that internally some information is collected (and discarded). Over the years there has been some demand to ignore AccessDenied and return a partial result instead:

In the past I explained why letting AccessDenied propagate is a good thing (see #2124 (comment)). Nevertheless, the use case to be able to retrieve a partial result instead of AccessDenied appears justified.

Proposal

The proposal is to identify for which APIs it make sense to do this, and either:

  1. introduce an ignore_eperm (or similar) parameter to the function signatures that require it, e.g.
p = psutil.Process(pid)
files = p.open_files(ignore_eperm=True)
  1. add a partial_result attribute to the AccessDenied instance, so that one can do:
p = psutil.Process(pid)
try:
    files = p.open_files()
except psutil.AccessDenied as err:
    files = err.partial_result

The first approach seems shorter and cleaner, but it requires changing the signature of multiple APIs (and the doc must reflect that).
The second approach appears more generic, but it's probably harder to understand for non-experts. Also, I guess it's too generic, because only certain APIs will set partial_result, so one may erroneously use the try/except idiom above and expect some result for an API which does not implement this contract.

It must be noted that also gopsutil project is puzzled by these sort of API design questions:

APIs

Potential API candidates are:

Process:

  • psutil.Process.children()
  • psutil.Process.connections()
  • psutil.Process.open_files()
  • psutil.Process.parents()
  • psutil.Process.threads()

System:

  • psutil.disk_io_counters()
  • psutil.disk_partitions()
  • psutil.net_connections()
  • psutil.net_io_counters()
  • psutil.users()
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

1 participant