From 686e6d50824fcc7403a51b91545899a6301d6216 Mon Sep 17 00:00:00 2001 From: Maximilian Nitsch Date: Mon, 25 Jul 2022 12:08:58 +0200 Subject: [PATCH] api-client: Break the receive loop if the VMM shuts down the socket Breaks the receive loop of the API client when the VMM shuts down the socket connection. A shutdown is indicated by the return value 0 of the `recv()` system call.[^1][^2] This case was not handled before, so the API client tried infinitely to receive more bytes and did not return. [^1]: https://linux.die.net/man/2/recv [^2]: https://doc.rust-lang.org/std/io/trait.Read.html#tymethod.read Signed-off-by: Maximilian Nitsch --- api_client/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/api_client/src/lib.rs b/api_client/src/lib.rs index 1c36b05ad..f9c9ff409 100644 --- a/api_client/src/lib.rs +++ b/api_client/src/lib.rs @@ -101,6 +101,10 @@ fn parse_http_response(socket: &mut dyn Read) -> Result, Error> { loop { let mut bytes = vec![0; 256]; let count = socket.read(&mut bytes).map_err(Error::Socket)?; + // If the return value is 0, the peer has performed an orderly shutdown. + if count == 0 { + break; + } res.push_str(std::str::from_utf8(&bytes[0..count]).unwrap()); // End of headers