From 7cc515749347afb821dc9937a48fc0b30d68ca37 Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Tue, 10 Sep 2024 13:06:46 +0300 Subject: [PATCH 1/7] extend the shutdown scope of `Ctx::stop` Signed-off-by: onur-ozkan --- mm2src/mm2_bin_lib/src/lib.rs | 2 +- mm2src/mm2_core/src/mm_ctx.rs | 15 ++++++++++++++- .../src/rpc/lp_commands/lp_commands_legacy.rs | 19 +------------------ 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/mm2src/mm2_bin_lib/src/lib.rs b/mm2src/mm2_bin_lib/src/lib.rs index 81c532419f..c78233e64a 100644 --- a/mm2src/mm2_bin_lib/src/lib.rs +++ b/mm2src/mm2_bin_lib/src/lib.rs @@ -99,5 +99,5 @@ fn prepare_for_mm2_stop() -> PrepareForStopResult { async fn finalize_mm2_stop(ctx: MmArc) { dispatch_lp_event(ctx.clone(), StopCtxEvent.into()).await; - let _ = ctx.stop(); + let _ = ctx.stop().await; } diff --git a/mm2src/mm2_core/src/mm_ctx.rs b/mm2src/mm2_core/src/mm_ctx.rs index e891f0b649..fe843b3eaf 100644 --- a/mm2src/mm2_core/src/mm_ctx.rs +++ b/mm2src/mm2_core/src/mm_ctx.rs @@ -500,7 +500,10 @@ lazy_static! { impl MmArc { pub fn new(ctx: MmCtx) -> MmArc { MmArc(SharedRc::new(ctx)) } - pub fn stop(&self) -> Result<(), String> { + pub async fn stop(&self) -> Result<(), String> { + #[cfg(not(target_arch = "wasm32"))] + try_s!(self.close_async_connection().await); + try_s!(self.stop.pin(true)); // Notify shutdown listeners. @@ -514,6 +517,16 @@ impl MmArc { Ok(()) } + #[cfg(not(target_arch = "wasm32"))] + async fn close_async_connection(&self) -> Result<(), db_common::async_sql_conn::AsyncConnError> { + if let Some(async_conn) = self.async_sqlite_connection.as_option() { + let mut conn = async_conn.lock().await; + conn.close().await?; + } + + Ok(()) + } + #[cfg(feature = "track-ctx-pointer")] fn track_ctx_pointer(&self) { let ctx_weak = self.weak(); diff --git a/mm2src/mm2_main/src/rpc/lp_commands/lp_commands_legacy.rs b/mm2src/mm2_main/src/rpc/lp_commands/lp_commands_legacy.rs index 2769539d9b..4ae6d0752a 100644 --- a/mm2src/mm2_main/src/rpc/lp_commands/lp_commands_legacy.rs +++ b/mm2src/mm2_main/src/rpc/lp_commands/lp_commands_legacy.rs @@ -21,7 +21,6 @@ use coins::{lp_coinfind, lp_coinfind_any, lp_coininit, CoinsContext, MmCoinEnum}; use common::executor::Timer; -use common::log::error; use common::{rpc_err_response, rpc_response, HyRes}; use futures::compat::Future01CompatExt; use http::Response; @@ -242,29 +241,13 @@ pub async fn my_balance(ctx: MmArc, req: Json) -> Result>, Stri Ok(try_s!(Response::builder().body(res))) } -#[cfg(not(target_arch = "wasm32"))] -async fn close_async_connection(ctx: &MmArc) { - if let Some(async_conn) = ctx.async_sqlite_connection.as_option() { - let mut conn = async_conn.lock().await; - if let Err(e) = conn.close().await { - error!("Error stopping AsyncConnection: {}", e); - } - } -} - pub async fn stop(ctx: MmArc) -> Result>, String> { dispatch_lp_event(ctx.clone(), StopCtxEvent.into()).await; // Should delay the shutdown a bit in order not to trip the "stop" RPC call in unit tests. // Stopping immediately leads to the "stop" RPC call failing with the "errno 10054" sometimes. let fut = async move { Timer::sleep(0.05).await; - - #[cfg(not(target_arch = "wasm32"))] - close_async_connection(&ctx).await; - - if let Err(e) = ctx.stop() { - error!("Error stopping MmCtx: {}", e); - } + ctx.stop().await.expect("Couldn't stop the KDF runtime."); }; // Please note we shouldn't use `MmCtx::spawner` to spawn this future, From 07b9a6de2514cd251fbde26e28a83bd192e0118b Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Tue, 10 Sep 2024 13:07:43 +0300 Subject: [PATCH 2/7] handle ctrl-c signals Signed-off-by: onur-ozkan --- mm2src/mm2_main/Cargo.toml | 2 +- mm2src/mm2_main/src/mm2.rs | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/mm2src/mm2_main/Cargo.toml b/mm2src/mm2_main/Cargo.toml index 60c3e9aa62..3013bce994 100644 --- a/mm2src/mm2_main/Cargo.toml +++ b/mm2src/mm2_main/Cargo.toml @@ -116,7 +116,7 @@ hyper = { version = "0.14.26", features = ["client", "http2", "server", "tcp"] } rcgen = "0.10" rustls = { version = "0.21", default-features = false } rustls-pemfile = "1.0.2" -tokio = { version = "1.20", features = ["io-util", "rt-multi-thread", "net"] } +tokio = { version = "1.20", features = ["io-util", "rt-multi-thread", "net", "signal"] } [target.'cfg(windows)'.dependencies] winapi = "0.3" diff --git a/mm2src/mm2_main/src/mm2.rs b/mm2src/mm2_main/src/mm2.rs index 7dcc5572cb..d8d02cedb4 100644 --- a/mm2src/mm2_main/src/mm2.rs +++ b/mm2src/mm2_main/src/mm2.rs @@ -43,6 +43,8 @@ #[cfg(not(target_arch = "wasm32"))] use common::block_on; use common::crash_reports::init_crash_reports; use common::double_panic_crash; +use common::executor::SpawnFuture; +use common::log; use common::log::LogLevel; use common::password_policy::password_policy; use mm2_core::mm_ctx::MmCtxBuilder; @@ -54,7 +56,6 @@ use lp_swap::PAYMENT_LOCKTIME; use std::sync::atomic::Ordering; use gstuff::slurp; - use serde::ser::Serialize; use serde_json::{self as json, Value as Json}; @@ -64,7 +65,6 @@ use std::process::exit; use std::ptr::null; use std::str; -mod lp_native_dex; pub use self::lp_native_dex::init_hw; pub use self::lp_native_dex::lp_init; use coins::update_coins_config; @@ -75,6 +75,7 @@ use mm2_err_handle::prelude::*; pub mod heartbeat_event; pub mod lp_dispatcher; pub mod lp_message_service; +mod lp_native_dex; pub mod lp_network; pub mod lp_ordermatch; pub mod lp_stats; @@ -160,10 +161,28 @@ pub async fn lp_main( .with_datetime(datetime.clone()) .into_mm_arc(); ctx_cb(try_s!(ctx.ffi_handle())); + + #[cfg(not(target_arch = "wasm32"))] + spawn_ctrl_c_handler(ctx.clone()); + try_s!(lp_init(ctx, version, datetime).await); Ok(()) } +/// Handles CTRL-C signals and shutdowns the KDF runtime gracefully. +#[cfg(not(target_arch = "wasm32"))] +fn spawn_ctrl_c_handler(ctx: mm2_core::mm_ctx::MmArc) { + ctx.spawner().spawn(async move { + tokio::signal::ctrl_c() + .await + .expect("Couldn't listen for the CTRL-C signal."); + + log::info!("Wrapping things up and shutting down..."); + + ctx.stop().await.expect("Couldn't stop the KDF runtime."); + }); +} + fn help() { const HELP_MSG: &str = r#"Command-line options. The first command-line argument is special and designates the mode. From ae8d70e8a2d3cb557827493bfa9277aed7988335 Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Tue, 10 Sep 2024 13:29:55 +0300 Subject: [PATCH 3/7] add an important note Signed-off-by: onur-ozkan --- mm2src/mm2_main/src/mm2.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm2src/mm2_main/src/mm2.rs b/mm2src/mm2_main/src/mm2.rs index d8d02cedb4..4342383b90 100644 --- a/mm2src/mm2_main/src/mm2.rs +++ b/mm2src/mm2_main/src/mm2.rs @@ -170,6 +170,8 @@ pub async fn lp_main( } /// Handles CTRL-C signals and shutdowns the KDF runtime gracefully. +/// +/// It's important to spawn this task as soon as `Ctx` is in the correct state. #[cfg(not(target_arch = "wasm32"))] fn spawn_ctrl_c_handler(ctx: mm2_core::mm_ctx::MmArc) { ctx.spawner().spawn(async move { From f86dd5cf56d8e0580553a4b609f76098c9e53f59 Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Mon, 16 Sep 2024 14:41:09 +0300 Subject: [PATCH 4/7] spawn `spawn_ctrl_c_handler` outside the abortable pool Signed-off-by: onur-ozkan --- mm2src/mm2_main/src/mm2.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mm2src/mm2_main/src/mm2.rs b/mm2src/mm2_main/src/mm2.rs index 4342383b90..95b69ae541 100644 --- a/mm2src/mm2_main/src/mm2.rs +++ b/mm2src/mm2_main/src/mm2.rs @@ -43,7 +43,6 @@ #[cfg(not(target_arch = "wasm32"))] use common::block_on; use common::crash_reports::init_crash_reports; use common::double_panic_crash; -use common::executor::SpawnFuture; use common::log; use common::log::LogLevel; use common::password_policy::password_policy; @@ -174,7 +173,7 @@ pub async fn lp_main( /// It's important to spawn this task as soon as `Ctx` is in the correct state. #[cfg(not(target_arch = "wasm32"))] fn spawn_ctrl_c_handler(ctx: mm2_core::mm_ctx::MmArc) { - ctx.spawner().spawn(async move { + common::executor::spawn(async move { tokio::signal::ctrl_c() .await .expect("Couldn't listen for the CTRL-C signal."); From 7d0f619dc318ccabb0a5f6b46e2f4db5a8efe901 Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Wed, 18 Sep 2024 11:17:23 +0300 Subject: [PATCH 5/7] update release profile Signed-off-by: onur-ozkan --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index deec6b843f..9aa8602063 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,8 +58,8 @@ debug-assertions = false opt-level = 3 strip = true codegen-units = 1 -# lto = true -panic = "abort" +lto = true +panic = 'unwind' [profile.dev] opt-level = 0 From dc26779de9de664cc3d755ecd4ce790d3be844c2 Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Wed, 18 Sep 2024 11:20:26 +0300 Subject: [PATCH 6/7] call `dispatch_lp_event` before the termination Signed-off-by: onur-ozkan --- mm2src/mm2_main/src/mm2.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mm2src/mm2_main/src/mm2.rs b/mm2src/mm2_main/src/mm2.rs index 95b69ae541..fc107cb06c 100644 --- a/mm2src/mm2_main/src/mm2.rs +++ b/mm2src/mm2_main/src/mm2.rs @@ -173,6 +173,8 @@ pub async fn lp_main( /// It's important to spawn this task as soon as `Ctx` is in the correct state. #[cfg(not(target_arch = "wasm32"))] fn spawn_ctrl_c_handler(ctx: mm2_core::mm_ctx::MmArc) { + use crate::lp_dispatcher::{dispatch_lp_event, StopCtxEvent}; + common::executor::spawn(async move { tokio::signal::ctrl_c() .await @@ -180,6 +182,7 @@ fn spawn_ctrl_c_handler(ctx: mm2_core::mm_ctx::MmArc) { log::info!("Wrapping things up and shutting down..."); + dispatch_lp_event(ctx.clone(), StopCtxEvent.into()).await; ctx.stop().await.expect("Couldn't stop the KDF runtime."); }); } From a3791bdb115ac533ed6795151fc88cb1d974e056 Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Wed, 18 Sep 2024 12:58:41 +0300 Subject: [PATCH 7/7] revert LTO Signed-off-by: onur-ozkan --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 9aa8602063..f6f7f67e61 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,7 +58,7 @@ debug-assertions = false opt-level = 3 strip = true codegen-units = 1 -lto = true +# lto = true panic = 'unwind' [profile.dev]