Comments (10)
Done in #134, new functions are called *_raw
, e.g. Socket::new_raw
.
from socket2.
We should use cfg(accessible)
for this once stable: rust-lang/rust#64797.
from socket2.
Hmm, I understand the reasoning behind this, but I do think that 9 out of 10 times (if not more) you want to use the default flags.
So I would actually flip this: have Socket::new()
use the almost always desired flags, and have Socket::with_custom_flags()
for the other use-cases.
from socket2.
Hmm, I understand the reasoning behind this, but I do think that 9 out of 10 times (if not more) you want to use the default flags.
This is probably true.
So I would actually flip this: have
Socket::new()
use the almost always desired flags, and haveSocket::with_custom_flags()
for the other use-cases.
I still argue for keeping Socket::new
as a wrapper for socket(2)
. I see the crate socket2 as a small wrapper around the libc interface with some convenient functions added, such as Domain::for_address
, and I would argue that Socket::new_with_common_flags
is one of those convenience functions.
from socket2.
Hmm, another reason to be careful is that it will change the behavior of existing code. Currently, sockets are always created with the CLOEXEC
flag set. After the change the file descriptors will be leaked to child processes if the default new()
is used. Of course there is semver to signify breaking changes, but most people will just fix compilation errors rather than look in=depth what changed.
To me personally, it also feels wrong to create file descriptors without close-on-exec set by default. Leaking file descriptors is bad, so not a nice default.
Also, currently there isn't even a way to set the close-on-exec flag after creation for the user. At the very least that should be exposed for the user, so with_default_flag()
doesn't do magic the user can't reproduce without fiddling with the raw fd.
from socket2.
Hmm, another reason to be careful is that it will change the behavior of existing code. Currently, sockets are always created with the
CLOEXEC
flag set. After the change the file descriptors will be leaked to child processes if the defaultnew()
is used. Of course there is semver to signify breaking changes, but most people will just fix compilation errors rather than look in=depth what changed.To me personally, it also feels wrong to create file descriptors without close-on-exec set by default. Leaking file descriptors is bad, so not a nice default.
The thing is, in some cases that is exactly what you want. We should support cases where the child process can use the socket. To me this library should do the least surprising thing. Since the documentation says the function says it corresponds to socket(2)
and WSASocketW
(https://github.com/alexcrichton/socket2-rs/blob/b0f77842b3357dd571bb86b30b354bc55215f693/src/socket.rs#L106-L108), it should only call that function. That is true for all methods, but previously Socket::new
was an exception and it did additional stuff (such as setting CLOEXEC
).
Also, currently there isn't even a way to set the close-on-exec flag after creation for the user. At the very least that should be exposed for the user, so
with_default_flag()
doesn't do magic the user can't reproduce without fiddling with the raw fd.
Socket::set_cloexec
sets the FD_CLOEXEC
flag: https://github.com/alexcrichton/socket2-rs/blob/b0f77842b3357dd571bb86b30b354bc55215f693/src/sys/unix.rs#L375-L382, so no fiddling is required.
from socket2.
The thing is, in some cases that is exactly what you want. We should support cases where the child process can use the socket.
Even then the proper thing to do is to disable the flag after forking, before exec-ing the new binary (or to use dup2, but still after forking). That's the only race-free way to let child processes inherit the file descriptor (atleast on platforms that have SOCK_CLOEXEC
, without it nothing is race free of course).
Socket::set_cloexec
sets theFD_CLOEXEC
flag:
Ah, I looked on docs.rs where it doesn't show up since it isn't released yet.
Since the documentation says the function says it corresponds to
socket(2)
andWSASocketW
, it should only call that function. That is true for all methods, but previouslySocket::new
was an exception and it did additional stuff (such as settingCLOEXEC
).
Another exception was accept
, which also set the close-on-exec flag. I still believe that setting the close-on-exec flag is important enough to warrant such an exception, and I'd rather update documentation to explain it than remove the behaviour.
And socket2
is not just a wrapper around syscalls, it's also a portability layer for different platforms. To me, that means doing the best possible thing by default on the supported platforms, and not to let the user deal with the platform differences.
from socket2.
@de-vri-es what you propose as function name that only calls socket(2)
/WSASocketW
and nothing else? I might be able to be convinced to change Socket::new
to set the flags, if can come up with a better name then Socket::new_with_common_flags
for the "set common flags" method (which is a terrible name).
from socket2.
How about raw_new()
? This would also translate nicely to raw_accept()
and raw_pair()
, which have the same problem.
Or maybe flipped: new_raw()
, accept_raw()
and pair_raw()
.
from socket2.
Starting on implementing that suggestion in #117.
from socket2.
Related Issues (20)
- Socket::bind_device missing on linux platforms HOT 9
- Don't set CLOEXEC on Fuchsia HOT 2
- Add support for ancillary data receiving HOT 3
- Add SO_REUSEPORT support, currently only set_reuse_address. HOT 1
- Support `sockatmark()`-like functionality HOT 5
- support target ohos HOT 3
- Add CI support for ESP-IDF
- msghdr as a private field in MsgHdr/MsgMutHdr HOT 2
- Set Socket interface using interface index HOT 1
- What happened to the `RAW` socket type? HOT 1
- Question: Possible to support architectures that only provide the `std::net` interface (and only TCP)? HOT 4
- Windows: `sa_family_t` should use `ADDRESS_FAMILY` from `windows-sys` HOT 5
- nonblocking connect HOT 6
- Set `ss_len` when creating `SockAddr` from std HOT 1
- Why cannot find WASStartup and WSACleanup in source code? HOT 7
- bind_device succeeds, but subsequent sends fail with no such device or address. HOT 5
- QUESTION: How to special `laddr` when dialing connection? HOT 3
- Android support HOT 3
- don'tassign8080 port
- Currently socket2 has no way to set the DF bit on packets. The attached patch adds it. HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from socket2.