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][darwin][freebsd][linux][openbsd] Make process.Children not reliant on pgrep #1706

Merged
merged 1 commit into from
Sep 11, 2024

Conversation

Lomanic
Copy link
Collaborator

@Lomanic Lomanic commented Sep 7, 2024

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 #1698

@Lomanic
Copy link
Collaborator Author

Lomanic commented Sep 7, 2024

Tested only on linux and openbsd

Copy link
Owner

@shirou shirou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much! I have confirmed that the expected behavior occurs on FreeBSD 13.3 and Ubuntu 20.04. This means, in the current master branch implementation, where an error exit status 1 would be returned, nil is now returned.

I have added a comment regarding the removal of the error definition. I would appreciate it if you could consider it.

@@ -18,7 +18,6 @@ import (

var (
invoke common.Invoker = common.Invoke{}
ErrorNoChildren = errors.New("process does not have children")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that ErrorNoChildren is a practically impossible error definition because when there are no children, pgrep exits with status 1. Therefore, it would be fine to remove this definition. However, since this definition has already been published, removing it would cause compilation failures.

So, how about adding a comment like Deprecated: This error is practically non-occurring? This way, it will be noted in the documentation and flagged by some linters.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you suggest to still keep the variable definition, but to never return it when there are no child processes found? Or to still return it when there are no child processes?

Keep in mind the API contract was already broken as this error was never documented in the Children() function comment and more importantly it was only returned on linux.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you suggest to still keep the variable definition, but to never return it when there are no child processes found? Or to still return it when there are no child processes?

I am thinking of keeping this variable defined, and an empty list and nil would be returned. It means ErrorNoChildren is never used.

Keep in mind the API contract was already broken as this error was never documented in the Children() function comment and more importantly it was only returned on Linux.

You are indeed correct that the error is not mentioned in the Children() function comment. However, unfortunately, pkg.go.dev displays the Linux definition by default, and this variable is included in that documentation. There may be users who are only looking at this variable and use it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, done.

…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
Copy link
Owner

@shirou shirou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you so much for your awesome work!

@shirou shirou merged commit e89f21d into shirou:master Sep 11, 2024
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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