Tokio: no way to set read/write timeout for tokio's TcpStream

Created on 31 Jul 2018  路  9Comments  路  Source: tokio-rs/tokio

especially, the tokio's listener accpeted a new TcpStream, this stream can't configure

Most helpful comment

You can use the tokio-io-timeout crate to manage read/write timeouts: https://crates.io/crates/tokio-io-timeout

All 9 comments

AFAIK, read / write timeouts are a synchronous socket concept.

but when use tokio-codec, the server will keep a lots of half-open tcp streams for a long time(have keepalive), if the client not shutdown the stream, just shutdown the modem...

To close a socket, you drop the codec (which transitively drops the TcpStream).

Close the socket

of cause i know i could actively close the socket, i just want the tcp/ip stack automatic to find the half open connections faster

You can use the tokio-io-timeout crate to manage read/write timeouts: https://crates.io/crates/tokio-io-timeout

tokio-io-timeout crate fix my issue

It's 2020 now and async/await have been made stable, and I had same problem. Though timeout is a concept for synchronous socket, it's still convenient to use this in asynchronous world sometime. I found this issue through searching engine.

The crate tokio-io-timeout did not work for me (perhaps due to changes of scheduler). However, tokio::time::timeout exactly met my requirement. I'm to write down this here, and hope this helps other people having the same problem.

When using async/await you have the ability to kill any operation whatsoever, which is what tokio::time::timeout allows you to do. Of course, it can also wrap any other asynchronous operation, and will work on those other operations just as fine as it does for TCP streams.

Was this page helpful?
0 / 5 - 0 ratings