From 7d8363a761627c238b4a013397cf99f5d4cf258d Mon Sep 17 00:00:00 2001 From: Sirius DevOps Date: Fri, 11 Sep 2026 22:33:55 -0400 Subject: [PATCH] fix(tui): do not freeze the UI on chat send Enter used block_on(Node::send), which retries for 180s on the TUI thread so event::read never ran. Spawn send on the runtime, poll keys at 100ms, show sending/sent/send failed on the footer. --- README.md | 2 +- src/tui.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++----- tests/send.rs | 31 ++++++++++++++++++++++++++++++- 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b79b716..560b7c2 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ or `cargo run --release`. On start you are prompted for a store passphrase (echo off, like other CLI passwords) or set `ONIONWIRE_STORE_PASSPHRASE`. Empty passphrase is rejected; a wrong passphrase does not open chat. Then you should see `onionwire: bootstrapping Arti…` on stderr. Directory bootstrap is usually under a minute; the onion is ready once a probe connect works (combined Arti status may still say Bootstrapping). Fail closed at 360s. Data lives in `ONIONWIRE_HOME` if set, otherwise `~/.local/share/onionwire/` (`onionwire.db` + Arti state, mode 0700). First open creates an ed25519 identity key. That key **is** you. -Then `F2` to share your invite, `F3` to paste a friend’s. Mouse-select the `onionwire:v1:…` line to copy. Highlight them in the roster, type in the composer, Enter to send. Fail closed: if their onion is down, send fails — no outbox. +Then `F2` to share your invite, `F3` to paste a friend’s. Mouse-select the `onionwire:v1:…` line to copy. Highlight them in the roster, type in the composer, Enter to send. Footer shows `sending`, then `sent` or `send failed` — the TUI stays usable while Tor retries (up to 180s). Fail closed: if their onion is down, send fails — no outbox. ## Friends are keys diff --git a/src/tui.rs b/src/tui.rs index dda019d..a52e8e2 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -1,5 +1,6 @@ use std::io; -use std::sync::Arc; +use std::sync::{mpsc, Arc}; +use std::time::Duration; use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers}; use ratatui::layout::{Constraint, Layout, Rect}; @@ -19,6 +20,18 @@ const C_STALE: Color = Color::Rgb(210, 180, 80); const C_HL_BG: Color = Color::Rgb(48, 42, 28); const C_BORDER: Color = Color::Rgb(72, 84, 64); +/// How long the TUI waits for a key before redrawing (so background send can update status). +pub const TUI_POLL: Duration = Duration::from_millis(100); + +/// Non-blocking read of a background send/job status. Never waits on Tor. +pub fn take_job_result(rx: &mpsc::Receiver) -> Option { + match rx.try_recv() { + Ok(s) => Some(s), + Err(mpsc::TryRecvError::Empty) => None, + Err(mpsc::TryRecvError::Disconnected) => Some("send failed".into()), + } +} + pub fn fingerprint_mismatch_banner() -> &'static str { "fingerprint mismatch" } @@ -608,6 +621,7 @@ struct App { help_open: bool, who_open: bool, chat_scroll: u16, + job_rx: Option>, } enum Screen { @@ -666,12 +680,17 @@ impl App { help_open: false, who_open: false, chat_scroll: 0, + job_rx: None, }) } fn run(&mut self, terminal: &mut DefaultTerminal) -> Result { loop { + self.drain_job(); terminal.draw(|f| self.draw(f)).map_err(|e| e.to_string())?; + if !event::poll(TUI_POLL).map_err(io_err)? { + continue; + } let Event::Key(key) = event::read().map_err(io_err)? else { continue; }; @@ -1234,14 +1253,34 @@ impl App { self.status_note = Some("no friend selected".into()); return Ok(()); }; - let pk = friend.pubkey.clone(); - match self.rt.block_on(self.node.send(&pk, text.as_bytes())) { - Ok(()) => self.status_note = Some("sent".into()), - Err(e) => self.status_note = Some(format!("send failed: {e}")), + if self.job_rx.is_some() { + self.status_note = Some("already sending".into()); + return Ok(()); } + let pk = friend.pubkey.clone(); + let body = text.as_bytes().to_vec(); + let node = Arc::clone(&self.node); + let (tx, rx) = mpsc::channel(); + self.status_note = Some("sending".into()); + std::mem::drop(self.rt.spawn(async move { + let note = match node.send(&pk, &body).await { + Ok(()) => "sent".to_string(), + Err(e) => format!("send failed: {e}"), + }; + let _ = tx.send(note); + })); + self.job_rx = Some(rx); Ok(()) } + fn drain_job(&mut self) { + let Some(note) = self.job_rx.as_ref().and_then(take_job_result) else { + return; + }; + self.status_note = Some(note); + self.job_rx = None; + } + fn who_body(&self) -> String { let Some(friend) = self.friends.get(self.selected) else { return "no friend selected\n\nEsc closes".into(); diff --git a/tests/send.rs b/tests/send.rs index 8aedab5..ce5341a 100644 --- a/tests/send.rs +++ b/tests/send.rs @@ -1,6 +1,10 @@ //! Composer Enter must send chat to the selected friend, not only slash commands. -use onionwire::tui::{composer_enter, ComposerAction, SlashCmd, WipeKind}; +use onionwire::tui::{ + composer_enter, take_job_result, ComposerAction, SlashCmd, WipeKind, TUI_POLL, +}; +use std::sync::mpsc; +use std::time::{Duration, Instant}; #[test] fn enter_plain_text_is_send() { @@ -43,3 +47,28 @@ fn enter_unknown_slash_is_not_chat() { Some(ComposerAction::UnknownSlash("/nope".into())) ); } + +#[test] +fn take_job_result_is_nonblocking_when_empty() { + let (tx, rx) = mpsc::channel::(); + let start = Instant::now(); + assert_eq!(take_job_result(&rx), None); + assert!( + start.elapsed() < Duration::from_millis(50), + "empty job rx must not wait on the 180s send retry" + ); + drop(tx); +} + +#[test] +fn take_job_result_reads_status_without_blocking() { + let (tx, rx) = mpsc::channel(); + tx.send("sent".into()).unwrap(); + assert_eq!(take_job_result(&rx).as_deref(), Some("sent")); +} + +#[test] +fn tui_poll_is_short_so_keys_keep_working() { + assert!(TUI_POLL <= Duration::from_millis(200)); + assert!(TUI_POLL > Duration::ZERO); +}