BufReader :: lines () over TcpStream stops iteration

I have a fairly simple code that uses TcpStreamand SslStreamaround it, reading a socket line by line with BufReader. Once upon a time, an iterator simply stops returning any data with Ok(0):

let mut stream = TcpStream::connect((self.config.host.as_ref(), self.config.port)).unwrap();
if self.config.ssl {
    let context = ssl::SslContext::new(ssl::SslMethod::Tlsv1_2).unwrap();
    let mut stream = ssl::SslStream::connect(&context, stream).unwrap();
    self.stream = Some(ssl::MaybeSslStream::Ssl(stream));
} else {
    self.stream = Some(ssl::MaybeSslStream::Normal(stream));
}

...

let read_stream = clone_stream(&self.stream);

let line_reader = BufReader::new(read_stream);

for line in line_reader.lines() {
    match line {
        Ok(line) => {
            ...
        }
        Err(e) => panic!("line read failed: {}", e),
    }
}
println!("lines out, {:?}", self.stream);

The loop just stops randomly as far as I can see, and there is no reason to believe that the socket was closed on the server side. A call self.stream.as_mut().unwrap().read_to_end(&mut buf)after loop completion returns Ok(0).

Any tips on how this will be handled? I do not receive any Err, so I can assume that the socket is still alive, but then I can not read anything from it. What is the current state of the socket and how should I proceed?

PS: clone_stream , .

fn clone_stream(stream: &Option<ssl::MaybeSslStream<TcpStream>>) -> ssl::MaybeSslStream<TcpStream> {
    if let &Some(ref s) = stream {
        match s {
            &ssl::MaybeSslStream::Ssl(ref s) => ssl::MaybeSslStream::Ssl(s.try_clone().unwrap()),
            &ssl::MaybeSslStream::Normal(ref s) => ssl::MaybeSslStream::Normal(s.try_clone().unwrap()),
        }
    } else {
        panic!();
    }
}
+4
1

, "- " , , , ( CLOSE_WAIT).

, :

stream.set_read_timeout(Some(Duration::new(60*5, 0)));
stream.set_write_timeout(Some(Duration::new(60*5, 0)));

- ErrorKind::WouldBlock -, ping , , .

+1

Source: https://habr.com/ru/post/1606149/


All Articles