Skip to content

Commit

Permalink
extend the shutdown scope of Ctx::stop
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <[email protected]>
  • Loading branch information
onur-ozkan committed Sep 10, 2024
1 parent c366630 commit 7cc5157
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
2 changes: 1 addition & 1 deletion mm2src/mm2_bin_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
15 changes: 14 additions & 1 deletion mm2src/mm2_core/src/mm_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
Expand Down
19 changes: 1 addition & 18 deletions mm2src/mm2_main/src/rpc/lp_commands/lp_commands_legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -242,29 +241,13 @@ pub async fn my_balance(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>>, 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<Response<Vec<u8>>, 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,
Expand Down

0 comments on commit 7cc5157

Please sign in to comment.