Skip to content

Commit

Permalink
stability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelLieb committed Mar 3, 2024
1 parent b43ec90 commit 309d3dd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
}
22 changes: 17 additions & 5 deletions src/utils/lights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}

0 comments on commit 309d3dd

Please sign in to comment.