-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
add KillError.NoSuchProcess #16816
add KillError.NoSuchProcess #16816
Conversation
Correct me if I'm wrong, but if the parent process cannot reserve that process id from being re-used by a completely unrelated process, this mechanism is unreliable: If the daemon process has quit and an unrelated process has started up, the OS may have reassigned the same process id. This would make the parent process think that the daemon is still running, when it's actually now a completely different program. I imagine that is the reason that |
I am not here to correct anyone or debate the efficacy of a technique that programmer B thinks is unreliable and programmer A should not do. But to address your criticisms, the amount of max pid that my system can have can be found at If the programmer is that paranoid or if the service is really that critical then yeah don't use that method, however, there are additional steps that could be taken to verify that PID X is a running process of the TYPE daemon right 🤔? Regardless of my toy example of how I would like use this side effect and why I wrote this two line fix, the real issue and I thought it was obvious, is how trivial it is to hit the Sorry I have been working on creative writing and so I was capturing a moment of inspiration while the ole brain juices were flowing. Anyways, I hope the wall of text above illustrates my points of reductio ad absurdum. The real issue here is not how the user's implementation stiches the features together. Its the fact that the TDLR; if (os.kill(my_pid, my_sig)) |_| {
// ... do my cool programmer stuff in here
} else |err| {
// this block is unreachable and an unavoidable program crash occurs
// if there is no process running with the pid of `my_pid`
debug.print(s_fmt, .{ "error type", @errorName(err) });
} meanwhile... #include <stdio.h>
#include <errno.h>
#include <signal.h>
int main() {
int rc = kill(my_pid, my_sig);
printf("return code %d\n", rc);
if (rc == -1) {
if (errno == ESRCH) {
printf("No such process [ESRCH %d]\n", errno);
// https://www.youtube.com/watch?v=OynKbfEGV_A
}
} else {
// ... do my cool programmer stuff in here
}
} |
Please do tell me if I'm wrong about something though, I like learning things. To the more general point, I imagine we could add an |
That is what pid 1 is for and why pid 1 has (daemonized) supervision capabilities. What is your specific use case, say for portability, to not use pid 1?
At least on Linux the proper way to do it is to set the uuid beforehand https://lwn.net/Articles/754980/:
So one needs semi-aweful hacks to kill reliably (rogue) processes. |
So I am wrong. @matu3ba I have used a variation of that technique in containers where restarting the container is not preferable to just attempting to manage a process life cycle. The article you linked is indeed very interesting, clearly their need for a reaper comes at the problem from a security perspective, my needs are not that serious. However, I did feel inspired by that article. Thanks for sharing!
Oh believe me I am fully aware of that option and quite frequently take that approach. However, that is typically done with a third party library more so than a I am gonna close this PR as Like you need a var d = fs.Dir{ .fd = undefined };
var fp = try d.open("/path/to/file", .{ .access_sub_paths = true });
// which instance is the one that is open here?
// and which do I need to close, both?
// I assume that defer close is safe even if you call close prior to the stack poping
if (fp.createFile("name of file", flags)) |file| {
//...
}
// what happens when I want to create a file in a another directory?
// if I want to reused the handle, do I need to close the directory first and then another call to open?
// I suppose I could create a whole new handle and just disregard the old one
// but what is the idiomatic approach? |
Feel free to also re-ask in one of the communities (f.e. the Zig Programming Language Discord has a dedicated
There's an outdated section on ziglearn that mostly goes over the output of Regarding your code snippet:
There is
As a general rule of thumb: If you directly initialize an aggregate, you know that initialization did nothing special, and so Also note that you can always call methods as functions on the struct: instead of initializing an Another hint: There's currently no
I think opening new handles is the way to go, but not sure I fully understand the scenario. Of course you can reuse the variables for handles: var current_dir = openStartDir();
defer current_dir.close(); //again don't close cwd() though (but explicitly opened `"."` should be okay afaik)
{
var swap = try openNextDir(current_dir);
current_dir.close();
current_dir = swap;
} |
WHAT
Adds
NoSuchProcess
to theKillError
error set instd.os.kill
, and returns that error whenerrno
is setESRCH
.WHY
Returning this error from
os.kill
is useful for determining if a process id is valid.# https://pubs.opengroup.org/onlinepubs/9699919799/ If sig is 0 (the null signal), error checking is performed but no signal is actually sent. The null signal can be used to check the validity of pid.
This facilitates a nice set of features in binaries that are meant to be executed as a daemon or background process. You can cache the daemon's pid to some type of storage at start up, and implement conditional branching with something such as cli arguments. A new process wants conditionally execute depending on a daemon's current state. i.e. (
start
,stop
,restart
,status
...) The process can then reach out to the storage location and pull up a pid of a potentially running daemon process and check with the operating system if that pid is still in use. Sending out signals is an asynchronous operation so super tight coordination is not reliable, however, this makes for a relatively simple low frequency heartbeat mechanism that can come in really handy.HOW