diff --git a/src/main.rs b/src/main.rs index 814713b..50c92d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,12 @@ mod utils; use std::error::Error; use std::sync::mpsc::channel; +use std::time::Duration; use crate::utils::audiodevices::create_monitor_stream; use crate::utils::config::{Config, ConfigError}; use log::{debug, error}; +use tokio::time::sleep; #[tokio::main] async fn main() { @@ -72,4 +74,6 @@ async fn main() { rx.recv().expect("Could not receive from channel."); println!("Shutting down"); drop(stream); + // Wait for proper shutdown + sleep(Duration::from_millis(100)).await; } diff --git a/src/utils/lights/mod.rs b/src/utils/lights/mod.rs index 4cb8512..fde4164 100644 --- a/src/utils/lights/mod.rs +++ b/src/utils/lights/mod.rs @@ -142,12 +142,24 @@ impl PollingHelper { impl Drop for PollingHelper { fn drop(&mut self) { - if let Ok(_) = self.tx.blocking_send(()) { - while !self.handle.is_finished() { - sleep(std::time::Duration::from_millis(10)); + // Check if Tokio is running + match tokio::runtime::Handle::try_current() { + Ok(handle) => { + let tx = self.tx.clone(); + handle.spawn(async move { tx.send(()).await }); + while !self.handle.is_finished() { + sleep(std::time::Duration::from_millis(10)); + } + } + Err(_) => { + if let Ok(_) = self.tx.blocking_send(()) { + while !self.handle.is_finished() { + sleep(std::time::Duration::from_millis(10)); + } + } else { + eprintln!("This should never happen"); + } } - } else { - eprintln!("This should never happen"); } } }