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] ppid() is incorrect in case of Process thread #2511

Open
giampaolo opened this issue Feb 27, 2025 · 1 comment
Open

[Linux] ppid() is incorrect in case of Process thread #2511

giampaolo opened this issue Feb 27, 2025 · 1 comment

Comments

@giampaolo
Copy link
Owner

giampaolo commented Feb 27, 2025

Note: Linux is the only platform allowing to instantiate a thread via Process class as in thread = Process(tid), and query the thread as if it was a process.

This test case fails:

    def test_thread_process_ppid(self):
        with ThreadTask():
            p1 = psutil.Process()
            threads = p1.threads()
            assert len(threads) == 2
            tid = threads[1].id
            p2 = psutil.Process(tid)
            assert p2.ppid() == os.getpid()

In this case p2.ppid() (name) is bash instead of python (which is "us", the process who actually started the thread).
Apparently, when Linux deals with a process thread, /proc/{TID}/status behaves differently in terms of Tgid and PPid columns. E.g. this is a normal process:

$ cat /proc/$PID/status
Name:   cat
Tgid:   125294
Pid:    125294
PPid:   22482

And this is a thread:

$ cat /proc/$TID/status
Name:	python3
Tgid:	125724
Pid:	125725
PPid:	125723

When Pid and Tgid are different, that indicates we're dealing with a thread (this I knew already). The problem is that PPid does not point to the process which spawned the thread ("python"in my case) but its parent ("sh"):

(Pdb++) psutil.Process(125723)  # PPid column returns 'sh' (wrong)
psutil.Process(pid=125723, name='sh', status='sleeping', started='15:17:43')
(Pdb++) psutil.Process(125724)  # Tgid columnt returns 'python3' (correct)
psutil.Process(pid=125724, name='python3', status='running', started='15:17:43')

This exact condition is also handled by forkstat util:
https://github.com/ColinIanKing/forkstat/blob/6d3bea2af4d7fa923b4647feb40244f154f9f5ad/forkstat.c#L700-L733
When dealing with a thread, it takes the ppid from Tgid column instead of PPid. This seems to return the correct result.

@giampaolo
Copy link
Owner Author

Also kind of related: #1989.

@giampaolo giampaolo changed the title [Linux] Process.ppid() is incorrect in case of Process thread [Linux] ppid() is incorrect in case of Process thread Feb 27, 2025
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