-
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
net/http: unix socket does not get removed when application exits, which makes restart fail #70985
Comments
This is how Unix domain sockets work - you have to delete the file before you can use the same address again. See https://man7.org/linux/man-pages/man7/unix.7.html
You should either do |
@rittneje The problem is really that most of the apps that use |
I didn't think there much we can do if you choose to use os.Exit or not allow cleanup to run by handling signals |
@seankhliao the first examples do not handle signals in any way and cleanup does not happen. That's why I'm reporting it, I actually found it out by just using gin that uses standard http.Serve with nothing special and cleanup doesn't happen on Ctrl+c |
@XANi It is not possible for any application to handle SIGKILL or sudden loss of power. Therefore, any robust application must arrange for the file to be deleted before calling Listen. Also, if you want SIGINT/SIGTERM to run any defers, then you need to handle these signals yourself instead of relying on the default behavior, which cannot know about any of your defers since it would be in a completely different goroutine. |
Okay, I didn't knew that is possible, how you can exit Go while triggering all defers ? Coz that would solve my problem. |
See |
@rittneje I asked about triggering defers (that might be dug 2 dependencies deep, like in case where I originally found that problem inside gin framework), I know how to handle signals |
Go version
go version go1.23.4 linux/amd64
Output of
go env
in your module/workspace:What did you do?
Running this code
over and over will work every time unless killed by SIGKILL because
u.Close()
cleans up the socket.but adding http.Serve in the mix:
will fail after first run because of leftover socket that now for some reason is not cleared by
u.Close()
. Even if cleanup is added manually bydefer os.Remove()
the result is still the same:
I even tried adding
but that didn't remove it either. Also tried adding signal handler so it gets clean
os.Exit
:with same result:
What did you see happen?
Socket not being removed when http.Serve uses it
What did you expect to see?
I'd expect one of 2 things to happen:
net.Listen
user pre-created socket if it can, which allows to, for example, set the permissions to the socket before listening on it which might desirable security-wiseThe text was updated successfully, but these errors were encountered: