Search code examples
rusttcprust-async-std

async-std::net:TcpStream read function does not return although data is available


Hi I have a strange bug that I am debugging for a few days now.

I am using async-std::net::TcpStream as part of a larger program like this:



// init socket and connection
let socket = std::net::SocketAddrV4::new(addr, port);
let stream = async_std::net::TcpStream::connect(socket).await?;

// get writer and reader
let mut reader = stream.clone();
let mut writer = stream;

// spawn writter
async_std::task::spawn(async move {
            Self::start_writer(&mut writer, cmd_rx, username, password, price_modify).await
        });

// set buffer
let mut buffer = bytes::BytesMut::with_capacity(8*1024);


// in loop read data and process it
 loop {
     println!("Read start");
     let n = reader.read(&mut buffer).await?;
    println!("Read end");

    // do stuff with data
}

This code worked fine for months for multiple connections but now I am connecting to new server and it stops randomly after few seconds to minutes. Always hangs on the read call. - I get "Read start" printout but no "Read end".

When I check what is happening in wireshark I can see packets coming in and TCP receive window starts decreasing until zero window is reached.

Must be something timing related as if I add to much debug printouts it works for longer time or even doesn't halt at all

When I change code so I use std::net::TcpStream instead of async-std::net::TcpStream everything works fine.

Difference between new and old connections is that new one is direct connection to NY server from London. Previous ones go through local repeater in London. Otherwise type of data and connection is the same.

Could this be bug in async-std? Anyone has an idea what to do next?


Solution

  • I gave up debugging async-std.

    I switched to tokio runtime and as expected everything works.

    As this and blocking calls work it looks like bug must be in async-std. I reported it on their project page.