Skip to content

Commit

Permalink
feat: fix base node shutdown (#6697)
Browse files Browse the repository at this point in the history
Description
---
Fixed the base node shutdown on Ubuntu (and hopefully on MacOS) whereas
pressing Ctrl-C multiple times rendered the command line interface in an
undefined state. This was not issue on Windows.

**Note:** The change to
'base_layer\core\src\base_node\tari_pulse_service\mod.rs' is an interim
change to enable testing (getting rid of the repeated command-line spam)
but not required for the base node to shut down. More changes in #6696.

Motivation and Context
---
The base node did not shut down properly on Ubuntu and MacOs.

How Has This Been Tested?
---
System-level testing on Windows and Ubuntu (WSL).

What process can a PR reviewer use to test or verify this change?
---
System-level testing - still outstanding on MacOS

<!-- Checklist -->
<!-- 1. Is the title of your PR in the form that would make nice release
notes? The title, excluding the conventional commit
tag, will be included exactly as is in the CHANGELOG, so please think
about it carefully. -->


Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify

<!-- Does this include a breaking change? If so, include this line as a
footer -->
<!-- BREAKING CHANGE: Description what the user should do, e.g. delete a
database, resync the chain -->
  • Loading branch information
hansieodendaal authored Nov 22, 2024
1 parent 0610bcc commit d22ef65
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
29 changes: 25 additions & 4 deletions applications/minotari_node/src/commands/cli_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use tokio::{signal, time};
use crate::{
commands::{
cli,
command::{CommandContext, WatchCommand},
command::{Args, CommandContext, WatchCommand},
parser::Parser,
reader::CommandReader,
},
Expand Down Expand Up @@ -87,7 +87,9 @@ impl CliLoop {
} else {
while !self.done {
self.watch_loop().await;
self.execute_command().await;
if !self.done {
self.execute_command().await;
}
}
}
}
Expand Down Expand Up @@ -129,12 +131,19 @@ impl CliLoop {
println!("Wrong command to watch `{}`. Failed with: {}", line, err);
} else {
let mut events = EventStream::new();
loop {
while !self.done {
let interval = time::sleep(interval);
tokio::select! {
_ = interval => {
if let Err(err) = self.context.handle_command_str(line).await {
println!("Watched command `{}` failed: {}", line, err);
} else {
let args: Result<Args, _> = line.parse();
if let Ok(command) = args {
if command.is_quit() {
self.done = true;
}
}
}
continue;
},
Expand All @@ -158,7 +167,9 @@ impl CliLoop {
}
}
}
crossterm::execute!(io::stdout(), cursor::MoveToNextLine(1)).ok();
if !self.done {
crossterm::execute!(io::stdout(), cursor::MoveToNextLine(1)).ok();
}
}
terminal::disable_raw_mode().ok();
}
Expand All @@ -183,6 +194,13 @@ impl CliLoop {
_ = interval => {
if let Err(err) = self.context.handle_command_str(line).await {
println!("Watched command `{}` failed: {}", line, err);
} else {
let args: Result<Args, _> = line.parse();
if let Ok(command) = args {
if command.is_quit() {
self.done = true;
}
}
}
continue;
},
Expand Down Expand Up @@ -242,6 +260,9 @@ impl CliLoop {
} else {
self.done = true;
}
if self.done && !self.shutdown_signal.is_triggered() {
self.context.shutdown.trigger();
}
},
_ = self.shutdown_signal.wait() => {
self.done = true;
Expand Down
6 changes: 6 additions & 0 deletions applications/minotari_node/src/commands/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ pub struct Args {
pub command: Command,
}

impl Args {
pub fn is_quit(&self) -> bool {
matches!(self.command, Command::Quit(_) | Command::Exit(_))
}
}

#[derive(Debug, Subcommand, EnumVariantNames)]
#[strum(serialize_all = "kebab-case")]
pub enum Command {
Expand Down
3 changes: 2 additions & 1 deletion applications/minotari_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ pub struct BaseNodeConfig {
pub state_machine: BaseNodeStateMachineConfig,
/// Obscure GRPC error responses
pub report_grpc_error: bool,
// Interval to check if the base node is still in sync with the network
/// Interval to check if the base node is still in sync with the network
#[serde(with = "serializers::seconds")]
pub tari_pulse_interval: Duration,
}

Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/base_node/tari_pulse_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ impl TariPulseService {
let mut interval = time::interval(self.config.check_interval);
let mut interval_failed = time::interval(Duration::from_millis(100));
loop {
interval.tick().await;
let passed_checkpoints = match self.passed_checkpoints(&mut base_node_service).await {
Ok(passed) => passed,
Err(err) => {
Expand All @@ -130,7 +131,6 @@ impl TariPulseService {
notify_passed_checkpoints
.send(!passed_checkpoints)
.expect("Channel should be open");
interval.tick().await;
}
}

Expand Down

0 comments on commit d22ef65

Please sign in to comment.