From 9e8a4efdb9b692ec6221c18003f54b9d3fdc4536 Mon Sep 17 00:00:00 2001 From: Sergio Lopez Date: Thu, 18 May 2023 12:08:17 +0200 Subject: [PATCH] vsock: close incoming UDS conns if we aren't ready It's possible to receive an incoming UDS connection before the VMM has contacted us to initialize the vrings. In this case, close the incoming connection so the client is aware of we aren't yet ready, and to avoid having a lingering incomplete connection around. Signed-off-by: Sergio Lopez --- crates/vsock/src/vhu_vsock_thread.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/vsock/src/vhu_vsock_thread.rs b/crates/vsock/src/vhu_vsock_thread.rs index ab61630..6d13e02 100644 --- a/crates/vsock/src/vhu_vsock_thread.rs +++ b/crates/vsock/src/vhu_vsock_thread.rs @@ -181,10 +181,9 @@ impl VhostUserVsockThread { fn handle_event(&mut self, fd: RawFd, evset: epoll::Events) { if fd == self.host_sock { // This is a new connection initiated by an application running on the host - self.host_listener - .accept() - .map_err(Error::UnixAccept) - .and_then(|(stream, _)| { + let conn = self.host_listener.accept().map_err(Error::UnixAccept); + if self.mem.is_some() { + conn.and_then(|(stream, _)| { stream .set_nonblocking(true) .map(|_| stream) @@ -194,6 +193,13 @@ impl VhostUserVsockThread { .unwrap_or_else(|err| { warn!("Unable to accept new local connection: {:?}", err); }); + } else { + // If we aren't ready to process requests, accept and immediately close + // the connection. + conn.map(drop).unwrap_or_else(|err| { + warn!("Error closing an incoming connection: {:?}", err); + }); + } } else { // Check if the stream represented by fd has already established a // connection with the application running in the guest