shirou / gopsutil

psutil for golang
Other
10.48k stars 1.58k forks source link

[process][unix] `err != nil` when calling process.Children() for a process without child processes #1698

Closed lichunqiang closed 4 days ago

lichunqiang commented 3 weeks ago

Describe the bug Calling Children() on a process without child processes returns a non-nil err

the source code uses common.CallPgrepWithContext which calls pgrep -P $PID. If input pid is without child processes, pgrep exit code is 1, the function call returns error with exits status 1

To Reproduce

// paste example code reproducing the bug you are reporting

Expected behavior The function should return ErrorNoChildren?

Environment (please complete the following information):

Additional context [Cross-compiling? Paste the command you are using to cross-compile and the result of the corresponding go env]

Lomanic commented 2 weeks ago

To me this was introduced in #1231 (meant to handle places where pgrep is missing).

As pgrep returns 1 (without any message in stderr/out) both when there's no child process found or the parent process doesn't exist (pgrep -P $$; echo $? vs pgrep -P 3; echo $?), gopsutil should get rid of pgrep and follow psutil, like this on linux (calls ppid() on all processes on all other posix OSes).

As an aside, I think this ErrorNoChildren error should not exist in the first place, the Children() function should simply return a nil error and an empty slice as this is not an error.