diff --git a/bin/src/daemon.rs b/bin/src/daemon.rs index 6749f21..9b6cce9 100644 --- a/bin/src/daemon.rs +++ b/bin/src/daemon.rs @@ -131,6 +131,7 @@ fn run_shutdown_thread( should_restart_flag: Arc, server_shutdown_tx: oneshot::Sender<()>, maybe_ui_shutdown_tx: Option>, + maybe_key_input_shutdown_tx: Option>, qmdl_store_lock: Arc>, analysis_tx: Sender, ) -> JoinHandle> { @@ -164,11 +165,11 @@ fn run_shutdown_thread( server_shutdown_tx .send(()) .expect("couldn't send server shutdown signal"); - info!("sending UI shutdown"); if let Some(ui_shutdown_tx) = maybe_ui_shutdown_tx { - ui_shutdown_tx - .send(()) - .expect("couldn't send ui shutdown signal"); + let _ = ui_shutdown_tx.send(()); + } + if let Some(key_input_shutdown_tx) = maybe_key_input_shutdown_tx { + let _ = key_input_shutdown_tx.send(()); } diag_device_sender .send(DiagDeviceCtrlMessage::Exit) @@ -217,6 +218,7 @@ async fn run_with_config( let (ui_update_tx, ui_update_rx) = mpsc::channel::(1); let (analysis_tx, analysis_rx) = mpsc::channel::(5); let mut maybe_ui_shutdown_tx = None; + let mut maybe_key_input_shutdown_tx = None; if !config.debug_mode { let (ui_shutdown_tx, ui_shutdown_rx) = oneshot::channel(); maybe_ui_shutdown_tx = Some(ui_shutdown_tx); @@ -242,7 +244,14 @@ async fn run_with_config( display::update_ui(&task_tracker, &config, ui_shutdown_rx, ui_update_rx); info!("Starting Key Input service"); - key_input::run_key_input_thread(&task_tracker, &config, diag_tx.clone()); + let (key_input_shutdown_tx, key_input_shutdown_rx) = oneshot::channel(); + maybe_key_input_shutdown_tx = Some(key_input_shutdown_tx); + key_input::run_key_input_thread( + &task_tracker, + &config, + diag_tx.clone(), + key_input_shutdown_rx, + ); } let (daemon_restart_tx, daemon_restart_rx) = oneshot::channel::<()>(); @@ -265,6 +274,7 @@ async fn run_with_config( should_restart_flag.clone(), server_shutdown_tx, maybe_ui_shutdown_tx, + maybe_key_input_shutdown_tx, qmdl_store_lock.clone(), analysis_tx.clone(), ); diff --git a/bin/src/key_input.rs b/bin/src/key_input.rs index 3577de4..765ace1 100644 --- a/bin/src/key_input.rs +++ b/bin/src/key_input.rs @@ -1,8 +1,9 @@ -use log::error; +use log::{error, info}; use std::time::{Duration, Instant}; use tokio::fs::File; use tokio::io::AsyncReadExt; use tokio::sync::mpsc::Sender; +use tokio::sync::oneshot; use tokio_util::task::TaskTracker; use crate::config; @@ -20,6 +21,7 @@ pub fn run_key_input_thread( task_tracker: &TaskTracker, config: &config::Config, diag_tx: Sender, + mut ui_shutdown_rx: oneshot::Receiver<()>, ) { if config.key_input_mode == 0 { return; @@ -40,9 +42,17 @@ pub fn run_key_input_thread( let mut last_event_time: Option = None; loop { - if let Err(e) = file.read_exact(&mut buffer).await { - error!("failed to read key input: {}", e); - return; + tokio::select! { + _ = &mut ui_shutdown_rx => { + info!("received key input shutdown"); + return; + } + result = file.read_exact(&mut buffer) => { + if let Err(e) = result { + error!("failed to read key input: {}", e); + return; + } + } } let event = parse_event(buffer);