Skip to content

Commit

Permalink
Gracefully shutdown if SIGINT was sent in TUI mode (#2784)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hashmap authored May 3, 2019
1 parent 85d06fe commit 55cbdf5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 38 deletions.
13 changes: 4 additions & 9 deletions servers/src/grin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
17 changes: 6 additions & 11 deletions src/bin/cmd/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {
Expand Down
23 changes: 5 additions & 18 deletions src/bin/tui/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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>,
Expand Down Expand Up @@ -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();*/
}
}
}
Expand All @@ -199,5 +185,6 @@ impl Controller {
next_stat_update = Utc::now().timestamp() + stat_update_interval;
}
}
server.stop();
}
}

0 comments on commit 55cbdf5

Please sign in to comment.