fix: run API server in LocalSet to fix spawn_local panic

Co-authored-by: aider (gemini/gemini-2.5-pro-preview-05-06) <aider@aider.chat>
This commit is contained in:
Chaos Rogers 2025-07-21 16:55:17 +01:00
parent c94e1ea4b0
commit 2d6f65046a

View file

@ -18,7 +18,7 @@ use std::{
sync::{Arc, Mutex, mpsc}, sync::{Arc, Mutex, mpsc},
thread, thread,
}; };
use tokio::task; use tokio::task::{self, LocalSet};
/// Embed the default config.json at compile time. /// Embed the default config.json at compile time.
const DEFAULT_CONFIG: &str = include_str!("../config.json"); const DEFAULT_CONFIG: &str = include_str!("../config.json");
@ -77,7 +77,11 @@ async fn main() {
}); });
} }
// 6⃣ Spawn the API server thread // 6⃣ Set up a LocalSet for the API server.
let local = LocalSet::new();
local
.run_until(async move {
// 7⃣ Spawn the API server thread
{ {
let api_state = ltc_state.clone(); let api_state = ltc_state.clone();
let offset_clone = hw_offset.clone(); let offset_clone = hw_offset.clone();
@ -88,11 +92,17 @@ async fn main() {
}); });
} }
// 7⃣ Keep main thread alive by processing LTC frames // 8⃣ Keep main thread alive by consuming LTC frames in a blocking task
println!("📡 Main thread entering loop..."); println!("📡 Main thread entering loop...");
let _ = task::spawn_blocking(move || {
// This will block the thread, but it's a blocking-safe thread.
for _frame in rx { for _frame in rx {
// no-op // no-op
} }
})
.await;
})
.await;
} }
#[cfg(test)] #[cfg(test)]