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,22 +77,32 @@ async fn main() {
}); });
} }
// 6⃣ Spawn the API server thread // 6⃣ Set up a LocalSet for the API server.
{ let local = LocalSet::new();
let api_state = ltc_state.clone(); local
let offset_clone = hw_offset.clone(); .run_until(async move {
task::spawn_local(async move { // 7⃣ Spawn the API server thread
if let Err(e) = start_api_server(api_state, offset_clone).await { {
eprintln!("API server error: {}", e); let api_state = ltc_state.clone();
let offset_clone = hw_offset.clone();
task::spawn_local(async move {
if let Err(e) = start_api_server(api_state, offset_clone).await {
eprintln!("API server error: {}", e);
}
});
} }
});
}
// 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...");
for _frame in rx { let _ = task::spawn_blocking(move || {
// no-op // This will block the thread, but it's a blocking-safe thread.
} for _frame in rx {
// no-op
}
})
.await;
})
.await;
} }
#[cfg(test)] #[cfg(test)]