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: Conn.File uses dup #5052
Labels
Comments
I realize that clearing O_NONBLOCK affects the original fd on many systems. File is meant for use passing a network connection as a file when exec'ing a subprocess. Other uses - such as for fiddling with flags - are explicitly not supported. That is what this part of the comment is saying: // The returned os.File's file descriptor is different from the connection's. // Attempting to change properties of the original using this duplicate // may or may not have the desired effect. In the context of passing an fd to a subprocess, we made the decision that it was important for the subprocess to start with its fd in blocking mode, so that non-network-aware subprocesses can still interact with network fds using ordinary (blocking) read and write calls. The code snippet you quoted makes clear that it understands the aliasing: // We want blocking mode for the new fd, hence the double negative. // This also puts the old fd into blocking mode, meaning that // I/O will block the thread instead of letting us use the epoll server. // Everything will still work, just with more threads. For what it's worth, the File method uses dup primarily so that the returned *os.File can be closed independently of the original net.Conn. Not doing so would create a situation where it is possible to close a net.Conn accidentally by closing the file, or vice versa, and it would be the only call anywhere that returns an open *os.File that must not be closed. Your report does not suggest any changes, so I am closing it as working as intended. Status changed to WorkingAsIntended. |
Ah, ok, so then it's slightly ambiguous comments (better on a second reading) combined with confusion on IRC. I blame Dave ;) 17:26 <davecheney> rcrowley: there is a note on that file that explains why we can't make the underlying fd available 17:27 <davecheney> (or *os.File) 17:27 <davecheney> because if the blocking mode is changed on that file, it will screw up the networking code That's what I started chasing down, saying "but dup won't protect you!". If this is as intended, then it's all good with me. (Personally, I'd expect if I .File() a socket, I shouldn't touch the original any more, so the "more threads" case shouldn't really trigger often.) |
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
The text was updated successfully, but these errors were encountered: