Skip to content

Commit 07b9a6d

Browse files
committedSep 10, 2024
handle ctrl-c signals
Signed-off-by: onur-ozkan <work@onurozkan.dev>
1 parent 7cc5157 commit 07b9a6d

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed
 

‎mm2src/mm2_main/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ hyper = { version = "0.14.26", features = ["client", "http2", "server", "tcp"] }
116116
rcgen = "0.10"
117117
rustls = { version = "0.21", default-features = false }
118118
rustls-pemfile = "1.0.2"
119-
tokio = { version = "1.20", features = ["io-util", "rt-multi-thread", "net"] }
119+
tokio = { version = "1.20", features = ["io-util", "rt-multi-thread", "net", "signal"] }
120120

121121
[target.'cfg(windows)'.dependencies]
122122
winapi = "0.3"

‎mm2src/mm2_main/src/mm2.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#[cfg(not(target_arch = "wasm32"))] use common::block_on;
4444
use common::crash_reports::init_crash_reports;
4545
use common::double_panic_crash;
46+
use common::executor::SpawnFuture;
47+
use common::log;
4648
use common::log::LogLevel;
4749
use common::password_policy::password_policy;
4850
use mm2_core::mm_ctx::MmCtxBuilder;
@@ -54,7 +56,6 @@ use lp_swap::PAYMENT_LOCKTIME;
5456
use std::sync::atomic::Ordering;
5557

5658
use gstuff::slurp;
57-
5859
use serde::ser::Serialize;
5960
use serde_json::{self as json, Value as Json};
6061

@@ -64,7 +65,6 @@ use std::process::exit;
6465
use std::ptr::null;
6566
use std::str;
6667

67-
mod lp_native_dex;
6868
pub use self::lp_native_dex::init_hw;
6969
pub use self::lp_native_dex::lp_init;
7070
use coins::update_coins_config;
@@ -75,6 +75,7 @@ use mm2_err_handle::prelude::*;
7575
pub mod heartbeat_event;
7676
pub mod lp_dispatcher;
7777
pub mod lp_message_service;
78+
mod lp_native_dex;
7879
pub mod lp_network;
7980
pub mod lp_ordermatch;
8081
pub mod lp_stats;
@@ -160,10 +161,28 @@ pub async fn lp_main(
160161
.with_datetime(datetime.clone())
161162
.into_mm_arc();
162163
ctx_cb(try_s!(ctx.ffi_handle()));
164+
165+
#[cfg(not(target_arch = "wasm32"))]
166+
spawn_ctrl_c_handler(ctx.clone());
167+
163168
try_s!(lp_init(ctx, version, datetime).await);
164169
Ok(())
165170
}
166171

172+
/// Handles CTRL-C signals and shutdowns the KDF runtime gracefully.
173+
#[cfg(not(target_arch = "wasm32"))]
174+
fn spawn_ctrl_c_handler(ctx: mm2_core::mm_ctx::MmArc) {
175+
ctx.spawner().spawn(async move {
176+
tokio::signal::ctrl_c()
177+
.await
178+
.expect("Couldn't listen for the CTRL-C signal.");
179+
180+
log::info!("Wrapping things up and shutting down...");
181+
182+
ctx.stop().await.expect("Couldn't stop the KDF runtime.");
183+
});
184+
}
185+
167186
fn help() {
168187
const HELP_MSG: &str = r#"Command-line options.
169188
The first command-line argument is special and designates the mode.

0 commit comments

Comments
 (0)