Skip to content

Commit

Permalink
dont use process abort on input thread error (#823)
Browse files Browse the repository at this point in the history
rather perform a graceful shutdown
  • Loading branch information
Stephan Dilly committed Aug 21, 2021
1 parent eef1a79 commit b1bacbe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

///
Expand Down
11 changes: 9 additions & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -33,6 +32,7 @@ pub struct Input {
desired_state: Arc<NotifyableMutex<bool>>,
current_state: Arc<AtomicBool>,
receiver: Receiver<InputEvent>,
aborted: Arc<AtomicBool>,
}

impl Input {
Expand All @@ -42,23 +42,26 @@ 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(&current_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);
}
});

Self {
receiver: rx,
desired_state,
current_state,
aborted,
}
}

Expand All @@ -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<Option<Event>> {
if event::poll(dur)? {
Ok(Some(event::read()?))
Expand Down

0 comments on commit b1bacbe

Please sign in to comment.