-
-
Notifications
You must be signed in to change notification settings - Fork 1.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
Sockets are inherited by subprocesses #14630
Comments
Reduced monkey-patch: require "socket"
module Crystal::System::Socket
private def create_handle(family, type, protocol, blocking) : Handle
{% if LibC.has_constant?(:SOCK_CLOEXEC) %}
type = type.to_i | LibC::SOCK_CLOEXEC
{% end %}
previous_def family, type, protocol, blocking
end
end |
Hm, this is actually a bit weird. There is a method crystal/src/crystal/system/unix/socket.cr Lines 17 to 23 in a30e3d9
This code was originally introduced in #1192 as I'd like to understand what's the reasoning behind this. It feels like this is handling the case where |
So you can always do the |
It's also to make it in one syscall, when possible, instead of two. That was completely overlooked. Searching the history doesn't bring much information, except that I participated in missing the whole thing (oops). File descriptors should always have CLOEXEC set by default (that, I remember). |
I just verified this seems to work correctly for |
Bug Report
Result
Reason
The first server socket is inherited by the subprocess and hence the port is still in use when the second socket is bound. Just like files are opened with
O_CLOEXEC
sockets should be opened with theSOCK_CLOEXEC
flag.https://www.man7.org/linux/man-pages/man2/socket.2.html
https://www.man7.org/linux/man-pages/man2/open.2.html
Monkey-patch fix
The text was updated successfully, but these errors were encountered: