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

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

Closed
5 tasks
lichunqiang opened this issue Aug 23, 2024 · 1 comment · Fixed by #1706
Closed
5 tasks

Comments

@lichunqiang
Copy link

lichunqiang commented Aug 23, 2024

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):

  • Windows: [paste the result of ver]
  • Linux: [paste contents of /etc/os-release and the result of uname -a]
  • Mac OS: [paste the result of sw_vers and uname -a
  • FreeBSD: [paste the result of freebsd-version -k -r -u and uname -a]
  • OpenBSD: [paste the result of uname -a]

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

@Lomanic Lomanic changed the title [pref] correct get process's children process result [process][unix] err != nil when calling process.Children() for a process without child processes Aug 31, 2024
@Lomanic
Copy link
Collaborator

Lomanic commented Aug 31, 2024

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.

@Lomanic Lomanic self-assigned this Sep 2, 2024
Lomanic added a commit to Lomanic/gopsutil that referenced this issue Sep 7, 2024
…reliant on pgrep

pgrep exits with an error of 1 (and nothing in stdout nor stderr) both if
a process doesn't exist or it doesn't have child processes, so we don't
use it anymore on these OSes.

Also remove the ErrorNoChildren error when there are no child processes,
this is erroneous (simply check for the length of the returned slice, plus
this is not an error per se), this was only returned on linux anyway.

Fixes shirou#1698
Lomanic added a commit to Lomanic/gopsutil that referenced this issue Sep 7, 2024
…reliant on pgrep

pgrep -P $PID exits with status of 1 (and nothing in stdout nor stderr) both if
a process doesn't exist or it doesn't have child processes, so we don't
use it anymore on these OSes.

Also remove the ErrorNoChildren error when there are no child processes,
this is erroneous (simply check for the length of the returned slice, plus
this is not an error per se), this was only returned on linux anyway.

Fixes shirou#1698
Lomanic added a commit to Lomanic/gopsutil that referenced this issue Sep 7, 2024
…reliant on pgrep

pgrep -P $PID exits with status of 1 (and nothing in stdout nor stderr) both if
a process doesn't exist or it doesn't have child processes, so we don't
use it anymore on these OSes. We sort PIDs as pgrep did.

Also remove the ErrorNoChildren error when there are no child processes,
this is erroneous (simply check for the length of the returned slice, plus
this is not an error per se), this was only returned on linux anyway.

Fixes shirou#1698
Lomanic added a commit to Lomanic/gopsutil that referenced this issue Sep 10, 2024
…reliant on pgrep

pgrep -P $PID exits with status of 1 (and nothing in stdout nor stderr) both if
a process doesn't exist or it doesn't have child processes, so we don't
use it anymore on these OSes. We sort PIDs as pgrep did.

Also deprecate the ErrorNoChildren error when there are no child processes,
this is erroneous (simply check for the length of the returned slice, plus
this is not an error per se), this was only returned on linux anyway.

Fixes shirou#1698
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants