From 49a1855cbb0b3511dfa157122b250d3f5a73b37b Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Sat, 21 Aug 2021 19:25:37 +0200 Subject: [PATCH] dont use process abort on input thread error (#823) rather perform a graceful shutdown --- src/app.rs | 4 ++-- src/input.rs | 11 +++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/app.rs b/src/app.rs index da46daf5..50defa26 100644 --- a/src/app.rs +++ b/src/app.rs @@ -395,8 +395,8 @@ impl App { } /// - pub const fn is_quit(&self) -> bool { - self.do_quit + pub fn is_quit(&self) -> bool { + self.do_quit || self.input.is_aborted() } /// diff --git a/src/input.rs b/src/input.rs index c2523aa2..3802e61c 100644 --- a/src/input.rs +++ b/src/input.rs @@ -3,7 +3,6 @@ use anyhow::Result; use crossbeam_channel::{unbounded, Receiver, Sender}; use crossterm::event::{self, Event}; use std::{ - process, sync::{ atomic::{AtomicBool, Ordering}, Arc, @@ -33,6 +32,7 @@ pub struct Input { desired_state: Arc>, current_state: Arc, receiver: Receiver, + aborted: Arc, } impl Input { @@ -42,16 +42,18 @@ impl Input { let desired_state = Arc::new(NotifyableMutex::new(true)); let current_state = Arc::new(AtomicBool::new(true)); + let aborted = Arc::new(AtomicBool::new(false)); let arc_desired = Arc::clone(&desired_state); let arc_current = Arc::clone(¤t_state); + let arc_aborted = Arc::clone(&aborted); thread::spawn(move || { if let Err(e) = Self::input_loop(&arc_desired, &arc_current, &tx) { log::error!("input thread error: {}", e); - process::abort(); + arc_aborted.store(true, Ordering::SeqCst); } }); @@ -59,6 +61,7 @@ impl Input { receiver: rx, desired_state, current_state, + aborted, } } @@ -82,6 +85,10 @@ impl Input { != self.current_state.load(Ordering::Relaxed) } + pub fn is_aborted(&self) -> bool { + self.aborted.load(Ordering::SeqCst) + } + fn poll(dur: Duration) -> anyhow::Result> { if event::poll(dur)? { Ok(Some(event::read()?))