From 55cbdf58fe48ca0ac1d2932dc13792c7ae637a36 Mon Sep 17 00:00:00 2001 From: hashmap Date: Fri, 3 May 2019 14:20:36 +0200 Subject: [PATCH] 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. --- servers/src/grin/server.rs | 13 ++++--------- src/bin/cmd/server.rs | 17 ++++++----------- src/bin/tui/ui.rs | 23 +++++------------------ 3 files changed, 15 insertions(+), 38 deletions(-) diff --git a/servers/src/grin/server.rs b/servers/src/grin/server.rs index 598def0212..d337c6bb77 100644 --- a/servers/src/grin/server.rs +++ b/servers/src/grin/server.rs @@ -75,12 +75,12 @@ impl Server { /// to poll info about the server status pub fn start(config: ServerConfig, mut info_callback: F) -> Result<(), Error> where - F: FnMut(Arc), + 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 diff --git a/src/bin/cmd/server.rs b/src/bin/cmd/server.rs index 0a21c51663..0cd8c6a88e 100644 --- a/src/bin/cmd/server.rs +++ b/src/bin/cmd/server.rs @@ -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| { - 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::start(config, |serv: servers::Server| { let running = Arc::new(AtomicBool::new(true)); let r = running.clone(); ctrlc::set_handler(move || { diff --git a/src/bin/tui/ui.rs b/src/bin/tui/ui.rs index dbc92900ad..c4f078a45d 100644 --- a/src/bin/tui/ui.rs +++ b/src/bin/tui/ui.rs @@ -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, @@ -166,29 +162,19 @@ impl Controller { let (tx, rx) = mpsc::channel::(); Ok(Controller { rx: rx, - ui: UI::new(tx.clone()), + ui: UI::new(tx), }) } /// Run the controller - pub fn run(&mut self, server: Arc, running: Arc) { + 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(); } }