Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit c574f0b

Browse files
Merge pull request #813 from MutinyWallet/sync-lock
Add async mutex around syncing
2 parents d68321d + 0170c23 commit c574f0b

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

mutiny-core/src/node.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use lightning::{
4141
use crate::multiesplora::MultiEsploraClient;
4242
use crate::utils::get_monitor_version;
4343
use bitcoin::util::bip32::ExtendedPrivKey;
44+
use futures_util::lock::Mutex;
4445
use lightning::events::bump_transaction::{BumpTransactionEventHandler, Wallet};
4546
use lightning::ln::PaymentSecret;
4647
use lightning::sign::{EntropySource, InMemorySigner, NodeSigner, Recipient};
@@ -162,6 +163,7 @@ pub(crate) struct Node<S: MutinyStorage> {
162163
wallet: Arc<OnChainWallet<S>>,
163164
logger: Arc<MutinyLogger>,
164165
pub(crate) lsp_client: Option<LspClient>,
166+
pub(crate) sync_lock: Arc<Mutex<()>>,
165167
stop: Arc<AtomicBool>,
166168
#[cfg(target_arch = "wasm32")]
167169
websocket_proxy_addr: String,
@@ -549,11 +551,14 @@ impl<S: MutinyStorage> Node<S> {
549551
keys_manager.get_node_id(Recipient::Node).unwrap()
550552
);
551553

554+
let sync_lock = Arc::new(Mutex::new(()));
555+
552556
// Here we re-attempt to persist any monitors that failed to persist previously.
553557
let retry_logger = logger.clone();
554558
let retry_persister = persister.clone();
555559
let retry_stop = stop.clone();
556560
let retry_chain_monitor = chain_monitor.clone();
561+
let retry_sync_lock = sync_lock.clone();
557562
utils::spawn(async move {
558563
// sleep 3 seconds before checking, we won't have any pending updates on startup
559564
sleep(3_000).await;
@@ -563,7 +568,11 @@ impl<S: MutinyStorage> Node<S> {
563568
break;
564569
}
565570

566-
let updates = retry_chain_monitor.list_pending_monitor_updates();
571+
let updates = {
572+
let _lock = retry_sync_lock.lock().await;
573+
retry_chain_monitor.list_pending_monitor_updates()
574+
};
575+
567576
for (funding_txo, update_ids) in updates {
568577
// if there are no updates, skip
569578
if update_ids.is_empty() {
@@ -642,6 +651,7 @@ impl<S: MutinyStorage> Node<S> {
642651
wallet,
643652
logger,
644653
lsp_client,
654+
sync_lock,
645655
stop,
646656
#[cfg(target_arch = "wasm32")]
647657
websocket_proxy_addr,

mutiny-core/src/nodemanager.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,14 @@ impl<S: MutinyStorage> NodeManager<S> {
13841384
async fn sync_ldk(&self) -> Result<(), MutinyError> {
13851385
let nodes = self.nodes.lock().await;
13861386

1387+
// Lock all the nodes so we can sync them, make sure we keep the locks
1388+
// in scope so they don't get dropped and unlocked.
1389+
let futs = nodes
1390+
.iter()
1391+
.map(|(_, node)| node.sync_lock.lock())
1392+
.collect::<Vec<_>>();
1393+
let _locks = join_all(futs).await;
1394+
13871395
let confirmables: Vec<&(dyn Confirm)> = nodes
13881396
.iter()
13891397
.flat_map(|(_, node)| {

0 commit comments

Comments
 (0)