don't block the runloop when shutting down

This commit is contained in:
Joe Ardent 2025-08-12 14:02:10 -07:00
parent b2de9352d3
commit 3dd7a7281b
4 changed files with 22 additions and 12 deletions

View file

@ -139,6 +139,11 @@ impl App {
KeyCode::Char('l') => self.logs(), KeyCode::Char('l') => self.logs(),
KeyCode::Char('m') => self.main(), KeyCode::Char('m') => self.main(),
_ => match mode { _ => match mode {
CurrentScreen::Main => {
if code == KeyCode::Char('d') {
self.service.refresh_peers().await;
}
}
CurrentScreen::Logging => match code { CurrentScreen::Logging => match code {
KeyCode::Left => change_log_level(-1), KeyCode::Left => change_log_level(-1),
KeyCode::Right => change_log_level(1), KeyCode::Right => change_log_level(1),
@ -168,7 +173,6 @@ impl App {
}, },
SendingScreen::Text => unreachable!(), SendingScreen::Text => unreachable!(),
}, },
CurrentScreen::Main => {}
CurrentScreen::Stopping => unreachable!(), CurrentScreen::Stopping => unreachable!(),
}, },
}, },

View file

@ -29,6 +29,7 @@ impl JocalService {
let handle = Handle::new(); let handle = Handle::new();
self.http_handle.get_or_init(|| handle.clone()); self.http_handle.get_or_init(|| handle.clone());
log::info!("starting http server"); log::info!("starting http server");
axum_server::bind_rustls(addr, ssl_config) axum_server::bind_rustls(addr, ssl_config)

View file

@ -32,8 +32,6 @@ pub const MULTICAST_IP: Ipv4Addr = Ipv4Addr::new(224, 0, 0, 167);
pub const LISTENING_SOCKET_ADDR: SocketAddrV4 = pub const LISTENING_SOCKET_ADDR: SocketAddrV4 =
SocketAddrV4::new(Ipv4Addr::from_bits(0), DEFAULT_PORT); SocketAddrV4::new(Ipv4Addr::from_bits(0), DEFAULT_PORT);
pub type ShutdownSender = mpsc::Sender<()>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Listeners { pub enum Listeners {
Udp, Udp,
@ -79,7 +77,7 @@ pub struct JocalService {
pub socket: Arc<UdpSocket>, pub socket: Arc<UdpSocket>,
pub client: reqwest::Client, pub client: reqwest::Client,
pub config: Config, pub config: Config,
http_handle: Arc<OnceLock<axum_server::Handle>>, http_handle: OnceLock<axum_server::Handle>,
// the receiving end will be held by the application so it can update the UI based on backend // the receiving end will be held by the application so it can update the UI based on backend
// events // events
transfer_event_tx: UnboundedSender<TransferEvent>, transfer_event_tx: UnboundedSender<TransferEvent>,

View file

@ -55,7 +55,9 @@ async fn start_and_run(terminal: &mut DefaultTerminal, config: Config) -> Result
let mut handles = JoinSet::new(); let mut handles = JoinSet::new();
app.service.start(&mut handles).await; app.service.start(&mut handles).await;
let mut alarm = tokio::time::interval(Duration::from_millis(200)); let mut tick = tokio::time::interval(Duration::from_millis(200));
let shutdown = shutdown(&mut handles);
let mut shutdown = std::pin::pin!(shutdown);
loop { loop {
terminal.draw(|frame| app.draw(frame))?; terminal.draw(|frame| app.draw(frame))?;
@ -63,12 +65,19 @@ async fn start_and_run(terminal: &mut DefaultTerminal, config: Config) -> Result
res = app.handle_events() => { res = app.handle_events() => {
res?; res?;
} }
_ = alarm.tick() => {} _ = tick.tick() => {}
} }
if app.screen() == CurrentScreen::Stopping { if app.screen() == CurrentScreen::Stopping {
tokio::select! {
_ = shutdown.as_mut() => {
break; break;
} }
_ = tick.tick() => {
log::info!("shutting down");
}
}
}
let peers = app.service.peers.lock().await; let peers = app.service.peers.lock().await;
app.peers.clear(); app.peers.clear();
@ -94,14 +103,12 @@ async fn start_and_run(terminal: &mut DefaultTerminal, config: Config) -> Result
} }
} }
shutdown(&mut handles).await;
Ok(()) Ok(())
} }
async fn shutdown(handles: &mut JoinSet<Listeners>) { async fn shutdown(handles: &mut JoinSet<Listeners>) {
let mut alarm = tokio::time::interval(Duration::from_secs(5)); let mut timeout = tokio::time::interval(Duration::from_secs(5));
alarm.tick().await; timeout.tick().await;
loop { loop {
tokio::select! { tokio::select! {
join_result = handles.join_next() => { join_result = handles.join_next() => {
@ -113,7 +120,7 @@ async fn shutdown(handles: &mut JoinSet<Listeners>) {
None => break, None => break,
} }
} }
_ = alarm.tick() => { _ = timeout.tick() => {
info!("Exit timeout reached, aborting all unjoined tasks"); info!("Exit timeout reached, aborting all unjoined tasks");
handles.abort_all(); handles.abort_all();
break; break;