From 718132b8b4e99ea9dca41292b1abbf355a57eb54 Mon Sep 17 00:00:00 2001 From: binarybaron Date: Fri, 9 Aug 2024 23:34:21 +0200 Subject: [PATCH] feat: Kill monero-wallet-rpc on GUI exit --- src-tauri/src/lib.rs | 16 +++++++++++++--- swap/src/api.rs | 25 +++++++++++++++++++------ swap/src/monero/wallet_rpc.rs | 6 +++++- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index d53a81e13..2655e3ebc 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -11,7 +11,7 @@ use swap::{ }, cli::command::{Bitcoin, Monero}, }; -use tauri::{Manager, State}; +use tauri::{Manager, RunEvent, State}; trait ToStringResult { fn to_string_result(self) -> Result; @@ -107,6 +107,16 @@ pub fn run() { withdraw_btc ]) .setup(setup) - .run(tauri::generate_context!()) - .expect("error while running tauri application"); + .build(tauri::generate_context!()) + .expect("error while building tauri application") + .run(|app, event| match event { + RunEvent::Exit | RunEvent::ExitRequested { .. } => { + let context = app.state::>().inner(); + + if let Err(err) = context.cleanup() { + println!("Cleanup failed {}", err); + } + } + _ => {} + }) } diff --git a/swap/src/api.rs b/swap/src/api.rs index 92eddc77f..75427810b 100644 --- a/swap/src/api.rs +++ b/swap/src/api.rs @@ -7,14 +7,14 @@ use crate::network::rendezvous::XmrBtcNamespace; use crate::protocol::Database; use crate::seed::Seed; use crate::{bitcoin, cli, monero}; -use anyhow::{bail, Context as AnyContext, Error, Result}; +use anyhow::{anyhow, bail, Context as AnyContext, Error, Result}; use futures::future::try_join_all; use std::fmt; use std::future::Future; use std::net::SocketAddr; use std::path::PathBuf; -use std::sync::{Arc, Once}; -use tokio::sync::{broadcast, broadcast::Sender, Mutex, RwLock}; +use std::sync::{Arc, Mutex, Once}; +use tokio::sync::{broadcast, broadcast::Sender, Mutex as TokioMutex, RwLock}; use tokio::task::JoinHandle; use url::Url; @@ -36,7 +36,7 @@ pub struct Config { use uuid::Uuid; #[derive(Default)] -pub struct PendingTaskList(Mutex>>); +pub struct PendingTaskList(TokioMutex>>); impl PendingTaskList { pub async fn spawn(&self, future: F) @@ -164,7 +164,7 @@ pub struct Context { pub db: Arc, bitcoin_wallet: Option>, monero_wallet: Option>, - monero_rpc_process: Option>, + monero_rpc_process: Option>>, pub swap_lock: Arc, pub config: Config, pub tasks: Arc, @@ -228,7 +228,7 @@ impl Context { db: open_db(data_dir.join("sqlite")).await?, bitcoin_wallet, monero_wallet, - monero_rpc_process: monero_rpc_process.map(Arc::new), + monero_rpc_process: monero_rpc_process.map(|prc| Arc::new(Mutex::new(prc))), config: Config { tor_socks5_port, namespace: XmrBtcNamespace::from_is_testnet(is_testnet), @@ -268,6 +268,19 @@ impl Context { tasks: Arc::new(PendingTaskList::default()), } } + + pub fn cleanup(&self) -> Result<()> { + if let Some(ref monero_rpc_process) = self.monero_rpc_process { + let mut process = monero_rpc_process + .lock() + .map_err(|_| anyhow!("Failed to lock monero_rpc_process for cleanup"))?; + + process.kill()?; + println!("Killed monero-wallet-rpc process"); + } + + Ok(()) + } } impl fmt::Debug for Context { diff --git a/swap/src/monero/wallet_rpc.rs b/swap/src/monero/wallet_rpc.rs index 609c3eabc..10c8a0959 100644 --- a/swap/src/monero/wallet_rpc.rs +++ b/swap/src/monero/wallet_rpc.rs @@ -8,12 +8,12 @@ use reqwest::header::CONTENT_LENGTH; use reqwest::Url; use serde::Deserialize; use sha2::{Digest, Sha256}; -use std::fmt; use std::fmt::{Debug, Display, Formatter}; use std::io::ErrorKind; use std::path::{Path, PathBuf}; use std::process::Stdio; use std::time::Duration; +use std::{fmt, io}; use tokio::fs::{remove_file, OpenOptions}; use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; use tokio::process::{Child, Command}; @@ -178,6 +178,10 @@ impl WalletRpcProcess { Url::parse(&format!("http://127.0.0.1:{}/json_rpc", self.port)) .expect("Static url template is always valid") } + + pub fn kill(&mut self) -> io::Result<()> { + self._child.start_kill() + } } pub struct WalletRpc {