diff --git a/Cargo.lock b/Cargo.lock index 839efa5f2..7491ee08b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1072,6 +1072,7 @@ version = "0.1.0" dependencies = [ "epoll 4.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "vhost_rs 0.1.0", "vm-memory 0.1.0 (git+https://github.com/rust-vmm/vm-memory)", "vm-virtio 0.1.0", diff --git a/vhost_user_backend/Cargo.toml b/vhost_user_backend/Cargo.toml index 42a34a355..666f583d9 100644 --- a/vhost_user_backend/Cargo.toml +++ b/vhost_user_backend/Cargo.toml @@ -12,6 +12,7 @@ mmio_support = ["vm-virtio/mmio_support"] [dependencies] epoll = ">=4.0.1" libc = "0.2.66" +log = "0.4.8" vm-memory = { git = "https://github.com/rust-vmm/vm-memory" } vm-virtio = { path = "../vm-virtio" } vmm-sys-util = ">=0.3.1" diff --git a/vhost_user_backend/src/lib.rs b/vhost_user_backend/src/lib.rs index fb8c7dc7e..c0db6a629 100644 --- a/vhost_user_backend/src/lib.rs +++ b/vhost_user_backend/src/lib.rs @@ -4,6 +4,9 @@ // Copyright 2019 Alibaba Cloud Computing. All rights reserved. // SPDX-License-Identifier: Apache-2.0 +#[macro_use] +extern crate log; + use std::error; use std::fs::File; use std::io; @@ -398,6 +401,7 @@ struct VhostUserHandler { max_queue_size: usize, memory: Option, vrings: Vec>>, + worker_thread: Option>>, } impl VhostUserHandler { @@ -421,10 +425,12 @@ impl VhostUserHandler { let vring_worker = Arc::new(VringWorker { epoll_fd }); let worker = vring_worker.clone(); - thread::Builder::new() - .name("vring_worker".to_string()) - .spawn(move || vring_worker.run(vring_handler)) - .map_err(VhostUserHandlerError::SpawnVringWorker)?; + let worker_thread = Some( + thread::Builder::new() + .name("vring_worker".to_string()) + .spawn(move || vring_worker.run(vring_handler)) + .map_err(VhostUserHandlerError::SpawnVringWorker)?, + ); Ok(VhostUserHandler { backend, @@ -437,6 +443,7 @@ impl VhostUserHandler { max_queue_size, memory: None, vrings, + worker_thread, }) } @@ -738,3 +745,13 @@ impl VhostUserSlaveReqHandler for VhostUserHandler { .map_err(VhostUserError::ReqHandlerError) } } + +impl Drop for VhostUserHandler { + fn drop(&mut self) { + if let Some(thread) = self.worker_thread.take() { + if let Err(e) = thread.join() { + error!("Error in vring worker: {:?}", e); + } + } + } +}