Comments (13)
surely to go from std to mio stream it is also unsafe and is likely, not sure that is the case, also done via file descriptors.
Why would this be unsafe?
However to go from std to mio is provided as a safe method in the mio interface but not the other way around. I have a use case where I need to clone tcp stream so that I can use a separate handle for the read thread vs write thread however because I use mio I end up fist creating a std stream using unsafe method and then cloning and then going back to mio using provided save method. Is it possible for mio to provide an api which includes a safe method to go to std or there is a reason it is left unsafe?
We simply didn't implement this, we add something like it though. I think we can implement From<mio::net::TcpStream> for std::net::TcpStream
.
from mio.
Hey, I would like to work on this.
I think we should also implement the From
trait for the other socket types as well? @Thomasdezeeuw, what's your opinion on this?
from mio.
Hey, I would like to work on this.
👍
I think we should also implement the
From
trait for the other socket types as well? @Thomasdezeeuw, what's your opinion on this?
Yes I think we can.
from mio.
Because your using FromRawFd::from_raw_fd
, which is unsafe.
from mio.
Perhaps I should have rephrased it as why is a save function not included in the mio implementation, surely to go from std to mio stream it is also unsafe and is likely, not sure that is the case, also done via file descriptors. However to go from std to mio is provided as a safe method in the mio interface but not the other way around. I have a use case where I need to clone tcp stream so that I can use a separate handle for the read thread vs write thread however because I use mio I end up fist creating a std stream using unsafe method and then cloning and then going back to mio using provided save method. Is it possible for mio to provide an api which includes a safe method to go to std or there is a reason it is left unsafe?
from mio.
That would be great!
from mio.
Pr for it would be welcome.
from mio.
Also worth noting that the reason I need to go back and forth between std & mio Steam is because I cannot clone a MIO stream, perhaps cloning it would also be a good idea to implement and not just going back and forth for the sake of using std Steam clone method.
from mio.
@softstream-link I wouldn't clone any file descriptors used with Mio. epoll(2)
on Linux does not handle cloned file descriptors properly and Mio can't do anything for this.
from mio.
What is the issue? Do you know where I can read more about it?
Because that is kind of exactly what I am doing and I have not seen the problem.
My steps are:
- accept non blocking Mio Stream
- convert to std like so
- try_clone using std Stream and convert using safe back to std like so
- Use one mio Stream to register with mio poll like so
- Use second mio Stream to only write from a separate user thread and never register with poll
Is the issue you are describing affecting epoll scenarios when you register both of the clones with mio poll or it also including my case?
from mio.
What is the issue? Do you know where I can read more about it?
See the manual page: https://www.man7.org/linux/man-pages/man7/epoll.7.html, can't link directly but see the "Will closing a file descriptor cause it to be removed from all epoll interest lists?" question in the Q&A section.
from mio.
See the manual page: https://www.man7.org/linux/man-pages/man7/epoll.7.html, can't link directly but see the "Will closing a file descriptor cause it to be removed from all epoll interest lists?" question in the Q&A section.
Keep in mind that this will only remove the one file descriptor and not the duplicate from the cloned TcpStream. Duplicating a file descriptor in linux will create a second FD to the same underlying handle in the kernel. When one of the FDs that are associated to the same handle is closed and removed from the poll set the other FD to the underlying handle and its epoll registration is still valid.
See the man page you linked under the Q&A section:
What happens if you register the same file descriptor on an
epoll instance twice?
You will probably get EEXIST. However, it is possible to add
a duplicate (dup(2), dup2(2), fcntl(2) F_DUPFD) file
descriptor to the same epoll instance. This can be a useful
technique for filtering events, if the duplicate file
descriptors are registered with different events masks.
from mio.
In my case it is only possible to close the original or clone by dropping the writer Stream which is the only available to the user and when it is dropped it issues a shutdown on the stream which I understand will propagate to clone per rust documentation, which in turn shall trigger the epool to indicate Readable event which yields no bytes, which means no further reads will produce data, at which point I also deregister the clone from the mio poll/backed by epoll on linux and that makes it ok, I think.
Do you think my logic is solid? There fore the issue describe does not apply to my case.
I wonder if that is the reason why there is no safe method to convert mio back to std, as it would open up this potential undefined behavior because std can clone.
from mio.
Related Issues (20)
- Document MSRV policy HOT 1
- Update socket types API to match std lib HOT 5
- unresolved imports in target xtensa-esp32-espidf HOT 7
- `syscall!` macros hide unsafe code HOT 2
- peek blocks after read on windows HOT 7
- Poll changing interest of server socket on Windows HOT 1
- Cannot continue listen! HOT 2
- Walker failure on Linux HOT 6
- Crash at GetQueuedCompletionStatusEx in dll HOT 4
- Is the implementation of the timer incomplete? HOT 1
- TCP listener via wasm HOT 1
- Compiling mio in AIX fails HOT 5
- sendmmsg / recvmmg support HOT 1
- Reading into BytesMut always returns 0 bytes read HOT 3
- README mentions a v1 that doesn't exist HOT 5
- Switch to libc for Hermit HOT 1
- Use std::os::fd on Hermit
- mio does not build for aarch64-apple-visionos HOT 8
- Conditional reregistering interests in `IoSourceState` with `mio_unsupported_force_poll_poll` poll(2) based on IO operation result HOT 5
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 mio.