Giter Club home page Giter Club logo

Comments (13)

Thomasdezeeuw avatar Thomasdezeeuw commented on June 22, 2024 1

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.

tglane avatar tglane commented on June 22, 2024 1

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.

Thomasdezeeuw avatar Thomasdezeeuw commented on June 22, 2024 1

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.

Thomasdezeeuw avatar Thomasdezeeuw commented on June 22, 2024

Because your using FromRawFd::from_raw_fd, which is unsafe.

from mio.

softstream-link avatar softstream-link commented on June 22, 2024

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.

softstream-link avatar softstream-link commented on June 22, 2024

That would be great!

from mio.

Thomasdezeeuw avatar Thomasdezeeuw commented on June 22, 2024

Pr for it would be welcome.

from mio.

softstream-link avatar softstream-link commented on June 22, 2024

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.

Thomasdezeeuw avatar Thomasdezeeuw commented on June 22, 2024

@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.

softstream-link avatar softstream-link commented on June 22, 2024

@Thomasdezeeuw

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:

  1. accept non blocking Mio Stream
  2. convert to std like so
  3. try_clone using std Stream and convert using safe back to std like so
  4. Use one mio Stream to register with mio poll like so
  5. 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.

Thomasdezeeuw avatar Thomasdezeeuw commented on June 22, 2024

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.

tglane avatar tglane commented on June 22, 2024

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.

softstream-link avatar softstream-link commented on June 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.