Skip to content

[process][darwin][freebsd][linux][openbsd] Make process.Children not reliant on pgrep - #1706

Merged
shirou merged 1 commit into
shirou:masterfrom
Lomanic:issue1698
Sep 11, 2024
Merged

[process][darwin][freebsd][linux][openbsd] Make process.Children not reliant on pgrep#1706
shirou merged 1 commit into
shirou:masterfrom
Lomanic:issue1698

Conversation

@Lomanic

@Lomanic Lomanic commented Sep 7, 2024

Copy link
Copy Markdown
Collaborator

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

Lomanic commented Sep 7, 2024

Copy link
Copy Markdown
Collaborator Author

Tested only on linux and openbsd

@shirou shirou left a comment

Copy link
Copy Markdown
Owner

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.

Comment thread process/process.go

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

@shirou shirou left a comment

Copy link
Copy Markdown
Owner

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants