async-std icon indicating copy to clipboard operation
async-std copied to clipboard

Add try_clone for TcpStream

Open atbentley opened this issue 5 years ago • 5 comments

From the discussion in #553

I also updated the client/server example to use try_clone instead of Arc, as well as added a small test.

atbentley avatar Dec 23 '19 02:12 atbentley

Not sure why this is failing on windows, is anyone able to point me in a direction here?

atbentley avatar Dec 23 '19 03:12 atbentley

@atbentley I'm unsure, but it seems this might be an issue in mio:

---- cloned_streams stdout ----
thread 'cloned_streams' panicked at 'cannot register an I/O event source: Os { code: 87, kind: Other, message: "The parameter is incorrect." }', src\libcore\result.rs:1188:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

We're not passing any extra parameters to the underlying OS primitives here; so it feels like it's coming from a lower level. This makes me wonder if going down the Arc route isn't the more reliable option.

yoshuawuyts avatar Dec 24 '19 14:12 yoshuawuyts

This could be a problem related to https://github.com/tokio-rs/tokio/issues/774

ghost avatar Jan 13 '20 18:01 ghost

Definitely looking forward to this. As it stands, I don’t see any way to read a socket on one thread and write it on another on rust stable. I’ve worked around it in the meantime using Arc::get_mut_unchecked, which feels pretty gross. It does seem to work fine, though.

jdafont avatar Jan 22 '20 04:01 jdafont

I attempted creating a std TcpStream, cloning it, and then using async_std::net::TcpStream's From<> impl to convert the std stream. I also received the error

thread 'main' panicked at 'cannot register an I/O event source: Os { code: 87, kind: Other, message: "The parameter is incorrect." }', C:\Users\jfont\.cargo\registry\src\github.com-1ecc6299db9ec823\async-std-1.4.0\src\net\driver\mod.rs:183:20

This happens after I've already converted the first one and I'm converting the second one. Here's the demonstration code:

let tcp_a = std::net::TcpStream::connect("127.0.0.1:43800")?;
let tcp_b = tcp_a.try_clone()?;
let reader = Arc::new(Mutex::new(async_std::net::TcpStream::from(tcp_a)));
let writer = Arc::new(Mutex::new(async_std::net::TcpStream::from(tcp_b)));

Here's the section relevant to the panic https://github.com/async-rs/async-std/blob/1ababac97fb4192d736a601d38d8665d7d3ec2da/src/net/driver/mod.rs#L181-L188

jdafont avatar Jan 24 '20 00:01 jdafont