Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Does tokio TcpStream require special cancellation handling?
I've got a tokio::net::TcpStream working inside the future given to a tokio::time::timeout() as follows:
async fn stream_comm(url: String, data: &Vec<u8>) -> Result<[u8; 512]> {
let mut stream = TcpStream::connect(url).await?;
stream.write_all(data).await?;
let buf: &mut [u8; 512] = &mut [0; 512];
stream.read(buf).await?;
...
Ok(*buf)
}
async fn monitored_comms(url: String, data: &Vec<u8>) {
match tokio::time::timeout(Duration::from_secs(5), stream_comm(url, data)).await {
Ok(_) => {dbg!("Completed on time");},
Err(elapsed) => {dbg!("Timed out! Elapsed says {:?}", elapsed);},
}
// TODO: cleanup?
}
The timeout docs say that when a future times out it is cancelled for you, but I'm worried about the AsyncWriteExt shutdown function -- do I need to call it explicitly someplace to fully release the stream resources (for a cancelled future or otherwise)? Would I need to do explicit cleanup work if I cancelled the future myself?
2 posts - 2 participants
🏷️ rust_feed