-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
os: FindProcess should document how to test if a process is alive since on UNIX it always returns a process #34396
Comments
Hey @srowles, thanks for taking the time to file this. I am not totally confident if the suggested example is within scope for |
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:
That doesn't really let you know if it's still running or not. Perhaps we could add a new Then what you could do: p, _ := os.FindProcess(123)
if p != nil && p.State().Running() {
....
}
} /cc @ianlancetaylor |
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. |
Given the number of times this has come up (here and on forums such as Stackoverflow), the suggestions for sending signal 0 which is in the user's way and requires them to go Googling, this issue is haunting me a little and I can't sleep thinking about it. I think that perhaps we could do more so os.FindProcess should do some more work on the UNIXes instead of blindly returning an unchecked process. I noticed that the now defunct CL https://codereview.appspot.com/7392048/ by Akshat Kumar which timed out had some of the changes that I was just thinking. My suggestions here would be that: and with that, perhaps we can turn this issue instead into a change/proposal for Go1.15. Kindly pinging @ianlancetaylor @mdempsky @randall77 as well to ask if this might seem like something viable. |
To me it seems like kind of an impossible problem. The program could test that the process exists, and then the process could exit, and then the program could try to do something with the process, but it wouldn't work. Or it would affect an entirely different process that was reusing the process ID. In general doing arbitrary things with a PID isn't safe on Unix systems. |
Not portable but Linux has pidfd: https://lwn.net/Articles/794707/ |
Indeed Ian, this is tricky if we are sending a signal to the process, but regardless of whether this might be racy or unsafe on UNIX systems, I think the same thing can happen on Windows but we strive hard to provide a FindProcess implementation for the user :) What we can do perhaps is document this unsafe behavior.
Cool, thanks for the reference, Brad! |
Change https://golang.org/cl/211801 mentions this issue: |
This is the first step to handle configuration reloading. With those changes, it is already possible to do so, by sending a SIGUSR2 signal to the Go server process. The next step would be to watch for changes to configuration files and call the Restart function of the GoUnicorn instance. SIGHUP is catched by the go server and forwarded as-is to gunicorn, which causes it to restart its workers. However, that does not trigger a reload of the Django settings, probably because they are already loaded in the master, before creating any of the worker instances. SIGUSR2, however, can be used to spawn a new gunicorn master process, but handling it is a bit trickier. Please refer to Gunicorn's documentation[0] for details, especially the "Upgrading to a new binary on the fly" section. As we are now effectively killing the gunicorn processed launched by the server, we need to handle some sort of check to make sure it is still running. That's done by using the already existing healthchecks, making them useful not only for the application start, but also for its lifetime. If a check is failed too many times in a given time period, the gunicorn processed is killed (if necessary) and then restarted. [0] https://docs.gunicorn.org/en/20.1.0/signals.html Other relevant links and documentation: Python library handling the processing swaping upon a SIGUSR2: https://github.com/flupke/rainbow-saddle/ Golang cannot easily check if a process exists on Unix systems: golang/go#34396 Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
The current implementation of always returning a process can lead to unexpected behavior and is generally not very helpful. This commit adds a simple check whether the process is still alive or not, as described in the POSIX standard, by attempting to send a null signal to the process. Fixes golang#34396 Link: https://pubs.opengroup.org/onlinepubs/9699919799/
The current implementation of always returning a process can lead to unexpected behavior and is generally not very helpful. This commit adds a simple check whether the process is still alive or not, as described in the POSIX standard, by attempting to send a null signal to the process. Fixes golang#34396 Link: https://pubs.opengroup.org/onlinepubs/9699919799/
Change https://go.dev/cl/494895 mentions this issue: |
The current implementation of always returning a process can lead to unexpected behavior and is generally not very helpful. This commit adds a simple check whether the process is still alive or not, as described in the POSIX standard, by attempting to send a null signal to the process. Fixes golang#34396 Link: https://pubs.opengroup.org/onlinepubs/9699919799/
The current implementation of always returning a process can lead to unexpected behavior and is generally not very helpful. This commit adds a simple check whether the process is still alive or not, as described in the POSIX standard, by attempting to send a null signal to the process. Fixes golang#34396 Link: https://pubs.opengroup.org/onlinepubs/9699919799/functions/kill.html
I would also mention that |
Using pidfd allows us to have a handle on the process and poll the handle to non-blocking wait for the process to exit. Fixes golang#34396 Fixes golang#60321 Fixes golang#60320
I made #60461 with implementation which uses pidfd. |
Using pidfd allows us to have a handle on the process and poll the handle to non-blocking wait for the process to exit. Fixes golang#34396 Fixes golang#60321 Fixes golang#60320
Change https://go.dev/cl/498615 mentions this issue: |
Using pidfd allows us to have a handle on the process and poll the handle to non-blocking wait for the process to exit. Fixes golang#34396 Fixes golang#60321 Fixes golang#60320
Change https://go.dev/cl/502815 mentions this issue: |
This is the first step to handle configuration reloading. With those changes, it is already possible to do so, by sending a SIGUSR2 signal to the Go server process. The next step would be to watch for changes to configuration files and call the Restart function of the GoUnicorn instance. SIGHUP is catched by the go server and forwarded as-is to gunicorn, which causes it to restart its workers. However, that does not trigger a reload of the Django settings, probably because they are already loaded in the master, before creating any of the worker instances. SIGUSR2, however, can be used to spawn a new gunicorn master process, but handling it is a bit trickier. Please refer to Gunicorn's documentation[0] for details, especially the "Upgrading to a new binary on the fly" section. As we are now effectively killing the gunicorn processed launched by the server, we need to handle some sort of check to make sure it is still running. That's done by using the already existing healthchecks, making them useful not only for the application start, but also for its lifetime. If a check is failed too many times in a given time period, the gunicorn processed is killed (if necessary) and then restarted. [0] https://docs.gunicorn.org/en/20.1.0/signals.html Other relevant links and documentation: Python library handling the processing swaping upon a SIGUSR2: https://github.com/flupke/rainbow-saddle/ Golang cannot easily check if a process exists on Unix systems: golang/go#34396 Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
What version of Go are you using (
go version
)?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
OutputWhat 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:
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:
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:
The text was updated successfully, but these errors were encountered: