Skip to content
This repository was archived by the owner on Oct 24, 2022. It is now read-only.

Commit a6c815d

Browse files
committed
Avoid unwrap() in VhostUserDaemon::start()
The SlaveListener::accept() may return Ok(None), handle this case to avoid unexpected panic. Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
1 parent f805c3e commit a6c815d

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/lib.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::fmt::{Display, Formatter};
1212
use std::sync::{Arc, Mutex};
1313
use std::thread;
1414

15-
use vhost::vhost_user::{Error as VhostUserError, Listener, SlaveListener};
15+
use vhost::vhost_user::{Error as VhostUserError, Listener, SlaveListener, SlaveReqHandler};
1616
use vm_memory::bitmap::Bitmap;
1717
use vm_memory::mmap::NewBitmap;
1818
use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap};
@@ -116,13 +116,12 @@ where
116116
///
117117
/// This runs in an infinite loop that should be terminating once the other end of the socket
118118
/// (the VMM) disconnects.
119+
// TODO: the current implementation has limitations that only one incoming connection will be
120+
// handled from the listener. Should it be enhanced to support reconnection?
119121
pub fn start(&mut self, listener: Listener) -> Result<()> {
120122
let mut slave_listener = SlaveListener::new(listener, self.handler.clone())
121123
.map_err(Error::CreateSlaveListener)?;
122-
let mut slave_handler = slave_listener
123-
.accept()
124-
.map_err(Error::CreateSlaveReqHandler)?
125-
.unwrap();
124+
let mut slave_handler = self.accept(&mut slave_listener)?;
126125
let handle = thread::Builder::new()
127126
.name(self.name.clone())
128127
.spawn(move || loop {
@@ -137,6 +136,19 @@ where
137136
Ok(())
138137
}
139138

139+
fn accept(
140+
&self,
141+
slave_listener: &mut SlaveListener<Mutex<VhostUserHandler<S, V, B>>>,
142+
) -> Result<SlaveReqHandler<Mutex<VhostUserHandler<S, V, B>>>> {
143+
loop {
144+
match slave_listener.accept() {
145+
Err(e) => return Err(Error::CreateSlaveListener(e)),
146+
Ok(Some(v)) => return Ok(v),
147+
Ok(None) => continue,
148+
}
149+
}
150+
}
151+
140152
/// Wait for the thread handling the vhost-user socket connection to terminate.
141153
pub fn wait(&mut self) -> Result<()> {
142154
if let Some(handle) = self.main_thread.take() {

0 commit comments

Comments
 (0)