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

os: FindProcess should document how to test if a process is alive since on UNIX it always returns a process #34396

Open
srowles opened this issue Sep 19, 2019 · 3 comments

Comments

@srowles
Copy link

commented Sep 19, 2019

What version of Go are you using (go version)?

$go version go1.12.6 linux/amd64

Does this issue reproduce with the latest release?

Yes, Documentation issue, see live doc at:

https://golang.org/pkg/os/#FindProcess

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
$ go env
GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="linux"

What did you do?

Tried to find out if a process exists using os.FindProcess()

Read the documentation to find that the FindProcess call always returns a nil error on unix systems so it doesn't really "find" the process:

On Unix systems, FindProcess always succeeds and returns a Process for the given pid, regardless of whether the process exists.

Found closed documentation ticket saying you can send a signal to find if the process really exists or not:

#14146 (comment)

What did you expect to see?

More helpful information in the documentation about how someone might actually test for a process existing, for example as a starter on Linux:

	// error is always nil on Unix systems, just creates a process object
	p, _ := os.FindProcess(pid)
	// send SIGCONT (on Linux) which is a safe signal to the process to test if it exists
	err = p.Signal(syscall.SIGCONT)

What did you see instead?

No help without how the user might find out that a process really exists. Just a comment that it doesn't actually tell you if the process exists:

On Unix systems, FindProcess always succeeds and returns a Process for the given pid, regardless of whether the process exists.
@toothrot

This comment has been minimized.

Copy link
Contributor

commented Sep 20, 2019

Hey @srowles, thanks for taking the time to file this.

I am not totally confident if the suggested example is within scope for os.FindProcess. /cc @bradfitz who chimed in last time this issue came up. #14146 (comment)

@toothrot toothrot added this to the Go1.14 milestone Sep 20, 2019
@bradfitz

This comment has been minimized.

Copy link
Member

commented Sep 20, 2019

I'm not sure SIGCONT is necessarily safe to send. It might be stopped on purpose, and then you just resumed it.

Maybe we need some sort of portable os.(*Process).State method? But then it'd be tempting to use ProcessState as its return type, which might not be a great fit, despite the name. Notably, its ExitCode method says:

ExitCode returns the exit code of the exited process, or -1 if the process hasn't exited or was terminated by a signal.

That doesn't really let you know if it's still running or not. Perhaps we could add a new Running() bool method.

Then what you could do:

    p, _ := os.FindProcess(123)
    if p != nil && p.State().Running() {
              ....
    }
}

/cc @ianlancetaylor

@srowles

This comment has been minimized.

Copy link
Author

commented Sep 20, 2019

I'd be happy with a new method, that would be preferable to the workaround of sending signals to try and determine the process state.

@odeke-em odeke-em changed the title os: documentation for os.FindProcess could be more helpful os: FindProcess should document how to test if a process is alive since on UNIX it always returns a process Sep 25, 2019
@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.