Skip to content

Commit

Permalink
Merge remote-tracking branch 'repo-b/kill-monero-wallet-rpc-on-quit'
Browse files Browse the repository at this point in the history
  • Loading branch information
Einliterflasche committed Aug 10, 2024
2 parents bf62cec + 718132b commit 3135e58
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
16 changes: 13 additions & 3 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use swap::{
},
cli::command::{Bitcoin, Monero},
};
use tauri::{Manager, State};
use tauri::{Manager, RunEvent, State};

trait ToStringResult<T> {
fn to_string_result(self) -> Result<T, String>;
Expand Down Expand Up @@ -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::<Arc<Context>>().inner();

if let Err(err) = context.cleanup() {
println!("Cleanup failed {}", err);
}
}
_ => {}
})
}
25 changes: 19 additions & 6 deletions swap/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -36,7 +36,7 @@ pub struct Config {
use uuid::Uuid;

#[derive(Default)]
pub struct PendingTaskList(Mutex<Vec<JoinHandle<()>>>);
pub struct PendingTaskList(TokioMutex<Vec<JoinHandle<()>>>);

impl PendingTaskList {
pub async fn spawn<F, T>(&self, future: F)
Expand Down Expand Up @@ -164,7 +164,7 @@ pub struct Context {
pub db: Arc<dyn Database + Send + Sync>,
bitcoin_wallet: Option<Arc<bitcoin::Wallet>>,
monero_wallet: Option<Arc<monero::Wallet>>,
monero_rpc_process: Option<Arc<monero::WalletRpcProcess>>,
monero_rpc_process: Option<Arc<Mutex<monero::WalletRpcProcess>>>,
pub swap_lock: Arc<SwapLock>,
pub config: Config,
pub tasks: Arc<PendingTaskList>,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion swap/src/monero/wallet_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3135e58

Please sign in to comment.