From 16a028f35d4690ecb0f8ef99cdba9d675f3424eb Mon Sep 17 00:00:00 2001 From: Viresh Kumar Date: Wed, 27 Oct 2021 12:14:11 +0530 Subject: [PATCH] [i2c] Avoid using expect() Be consistent with the use of expect() and unwrap() and use only one of them, if required. Also add a comment on why usage of unwrap() is safe in main(). Signed-off-by: Viresh Kumar --- src/i2c/src/main.rs | 15 +++++++++------ src/i2c/src/vhu_i2c.rs | 6 ++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/i2c/src/main.rs b/src/i2c/src/main.rs index 15a5cd4..9f9f2c4 100644 --- a/src/i2c/src/main.rs +++ b/src/i2c/src/main.rs @@ -182,6 +182,14 @@ fn start_backend(config: I2cConfiguration) let i2c_map = i2c_map.clone(); let handle = spawn(move || loop { + // A separate thread is spawned for each socket and can connect to a separate guest. + // These are run in an infinite loop to not require the daemon to be restarted once a + // guest exits. + // + // There isn't much value in complicating code here to return an error from the + // threads, and so the code uses unwrap() instead. The panic on a thread won't cause + // trouble to other threads/guests or the main() function and should be safe for the + // daemon. let backend = Arc::new(RwLock::new( VhostUserI2cBackend::new(i2c_map.clone()).unwrap(), )); @@ -211,12 +219,7 @@ fn start_backend(config: I2cConfiguration) } // No matter the result, we need to shut down the worker thread. - backend - .read() - .unwrap() - .exit_event - .write(1) - .expect("Shutting down worker thread"); + backend.read().unwrap().exit_event.write(1).unwrap(); }); handles.push(handle); diff --git a/src/i2c/src/vhu_i2c.rs b/src/i2c/src/vhu_i2c.rs index 0d96204..6c784da 100644 --- a/src/i2c/src/vhu_i2c.rs +++ b/src/i2c/src/vhu_i2c.rs @@ -51,6 +51,8 @@ pub enum Error { DescriptorWriteFailed, #[error("Failed to send notification")] NotificationFailed, + #[error("Failed to create new EventFd: {0:?}")] + EventFdFailed(std::io::Error), } impl convert::From for io::Error { @@ -94,7 +96,7 @@ impl VhostUserI2cBackend { i2c_map, event_idx: false, mem: None, - exit_event: EventFd::new(EFD_NONBLOCK).expect("Creating exit eventfd"), + exit_event: EventFd::new(EFD_NONBLOCK).map_err(Error::EventFdFailed)?, }) } @@ -295,7 +297,7 @@ impl VhostUserBackendMut } fn exit_event(&self, _thread_index: usize) -> Option { - Some(self.exit_event.try_clone().expect("Cloning exit eventfd")) + self.exit_event.try_clone().ok() } }