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.Process().ppid() works but .parent() doesn't #1905

Open
harplife opened this issue Jan 15, 2021 · 0 comments
Open

psutil.Process().ppid() works but .parent() doesn't #1905

harplife opened this issue Jan 15, 2021 · 0 comments

Comments

@harplife
Copy link

Summary

  • OS: Windows 10
  • Architecture: 64bit
  • Psutil version: 5.8
  • Python version: 3.7.4
  • Type:

Description

I'm trying to write a program that finds ALL parent processes of a process.

My code is this:

import psutil

def print_process_info(proc_obj, proc_order=1):
    with proc_obj.oneshot():
        print(f'process order {proc_order}')
        print('name: ', proc_obj.name())
        print('pid: ', proc_obj.pid)
        print('status: ', proc_obj.status())
        print('exe: ', proc_obj.exe())
        try:
            parent_pid = proc_obj.ppid()
        except Exception as e:
            raise e
        else:
            print('parent id: ', parent_pid)
            try:
                parent = proc_obj.parent()
                #parent = psutil.Process(parent_pid)
            except psutil.NoSuchProcess:
                print(f'Unable to find parent, {parent_pid}')
            else:
                if parent is not None:
                    print('\nPrinting parent process info\n')
                    proc_order += 1
                    print_process_info(parent, proc_order=proc_order)
                else:
                    print(f'Unable to find parent, {parent_pid}')

p = psutil.Process()

The problem I've noticed:

Once this program goes to what I think is the top-level,
it still returns a parent pid - but won't access the parent process.

For example, running this code returns a log similar to this:

  1. python, pid 49000, ppid 45636
  2. python, pid 45636, ppid 40208
  3. bash, pid 40208, ppid 21072
  4. bash, pid 21072, ppid 54124
  5. cmd, pid 54124, ppid 46588
  6. git-bash, pid 46588, ppid 10116
  7. explorer, pid 10116, ppid 10036

and then, it's unable to find a process with pid 10036.

Using .parent() simply returns None,
whereas .Process(ppid) returns an Exception NoSuchProcess.

My assumption for the cause of the problem is Access Permission,
but I'm confused because .ppid() should return with an Exception describing Access Denied if that was the case. This goes the same with .parent(), or .Process(ppid).

If this really is a problem related with access permission,

  • why would it not say so?
  • is there a way to get access permission?

If it's not, can anybody shed a light on what may be the problem?

@harplife harplife added the bug label Jan 15, 2021
rkooo567 added a commit to ray-project/ray that referenced this issue Oct 26, 2022
Currently there's not enough information when the agent shutdown. Also, it seems like psutil.parent is not always correct. So as short-term mitigation, I tried making termination more graceful (it has 5 loops (10 s) before it considers the parent as dead). Maybe it'd be better if we send a heartbeat than checking the process directly using psutil as well...

giampaolo/psutil#1905
WeichenXu123 pushed a commit to WeichenXu123/ray that referenced this issue Dec 19, 2022
…t#29540)

Currently there's not enough information when the agent shutdown. Also, it seems like psutil.parent is not always correct. So as short-term mitigation, I tried making termination more graceful (it has 5 loops (10 s) before it considers the parent as dead). Maybe it'd be better if we send a heartbeat than checking the process directly using psutil as well...

giampaolo/psutil#1905

Signed-off-by: Weichen Xu <weichen.xu@databricks.com>
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