Gracefully shutdown if SIGINT was sent in TUI mode (#2784)

Fixes #2799

Also 2 Arc's were replaced by one server's instance.
It is needed for p2p thread management in #2778, currently there is no point where we could store thread handles and join them because thread::join
consume the caller, which is impossible in case of Arc.
This commit is contained in:
hashmap 2019-05-03 14:20:36 +02:00 committed by GitHub
parent 85d06fe416
commit 55cbdf58fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 38 deletions

View file

@ -75,12 +75,12 @@ impl Server {
/// to poll info about the server status
pub fn start<F>(config: ServerConfig, mut info_callback: F) -> Result<(), Error>
where
F: FnMut(Arc<Server>),
F: FnMut(Server),
{
let mining_config = config.stratum_mining_config.clone();
let enable_test_miner = config.run_test_miner;
let test_miner_wallet_url = config.test_miner_wallet_url.clone();
let serv = Arc::new(Server::new(config)?);
let serv = Server::new(config)?;
if let Some(c) = mining_config {
let enable_stratum_server = c.enable_stratum_server;
@ -101,13 +101,8 @@ impl Server {
}
}
info_callback(serv.clone());
loop {
thread::sleep(time::Duration::from_secs(1));
if serv.stop_state.lock().is_stopped() {
return Ok(());
}
}
info_callback(serv);
Ok(())
}
// Exclusive (advisory) lock_file to ensure we do not run multiple

View file

@ -45,21 +45,16 @@ fn start_server_tui(config: servers::ServerConfig) {
// everything it might need
if config.run_tui.unwrap_or(false) {
warn!("Starting GRIN in UI mode...");
servers::Server::start(config, |serv: Arc<servers::Server>| {
let running = Arc::new(AtomicBool::new(true));
let _ = thread::Builder::new()
.name("ui".to_string())
.spawn(move || {
let mut controller = ui::Controller::new().unwrap_or_else(|e| {
panic!("Error loading UI controller: {}", e);
});
controller.run(serv.clone(), running);
});
servers::Server::start(config, |serv: servers::Server| {
let mut controller = ui::Controller::new().unwrap_or_else(|e| {
panic!("Error loading UI controller: {}", e);
});
controller.run(serv);
})
.unwrap();
} else {
warn!("Starting GRIN w/o UI...");
servers::Server::start(config, |serv: Arc<servers::Server>| {
servers::Server::start(config, |serv: servers::Server| {
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
ctrlc::set_handler(move || {

View file

@ -16,9 +16,6 @@
//! of various subsystems
use chrono::prelude::Utc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{mpsc, Arc};
use cursive::direction::Orientation;
use cursive::theme::BaseColor::{Black, Blue, Cyan, White};
use cursive::theme::Color::Dark;
@ -31,15 +28,14 @@ use cursive::traits::Identifiable;
use cursive::utils::markup::StyledString;
use cursive::views::{LinearLayout, Panel, StackView, TextView, ViewBox};
use cursive::Cursive;
use std::sync::mpsc;
use crate::built_info;
use crate::servers::Server;
use crate::tui::constants::ROOT_STACK;
use crate::tui::types::{TUIStatusListener, UIMessage};
use crate::tui::{menu, mining, peers, status, version};
use crate::built_info;
pub struct UI {
cursive: Cursive,
ui_rx: mpsc::Receiver<UIMessage>,
@ -166,29 +162,19 @@ impl Controller {
let (tx, rx) = mpsc::channel::<ControllerMessage>();
Ok(Controller {
rx: rx,
ui: UI::new(tx.clone()),
ui: UI::new(tx),
})
}
/// Run the controller
pub fn run(&mut self, server: Arc<Server>, running: Arc<AtomicBool>) {
pub fn run(&mut self, server: Server) {
let stat_update_interval = 1;
let mut next_stat_update = Utc::now().timestamp() + stat_update_interval;
while self.ui.step() {
if !running.load(Ordering::SeqCst) {
warn!("Received SIGINT (Ctrl+C).");
server.stop();
self.ui.stop();
}
while let Some(message) = self.rx.try_iter().next() {
match message {
ControllerMessage::Shutdown => {
server.stop();
self.ui.stop();
running.store(false, Ordering::SeqCst)
/*self.ui
.ui_tx
.send(UIMessage::UpdateOutput("update".to_string()))
.unwrap();*/
}
}
}
@ -199,5 +185,6 @@ impl Controller {
next_stat_update = Utc::now().timestamp() + stat_update_interval;
}
}
server.stop();
}
}