Skip to content

Commit d8e33c1

Browse files
committed
Fix circular Arc reference in LiquiditySource
`LiquiditySource` takes a reference to our `PeerManager` but the `PeerManager` holds an indirect reference to the `LiquiditySource`. As a result, after our `Node` instance is `stop`ped and the `Node` `drop`ped, much of the node's memory will stick around, including the `NetworkGraph`. Here we fix this issue by using `Weak` pointers, though note that there is another issue caused by LDK's gossip validation API.
1 parent 80a1745 commit d8e33c1

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@ fn build_with_store_internal(
16031603
peer_manager_clone.process_events();
16041604
}));
16051605

1606-
liquidity_source.as_ref().map(|l| l.set_peer_manager(Arc::clone(&peer_manager)));
1606+
liquidity_source.as_ref().map(|l| l.set_peer_manager(Arc::downgrade(&peer_manager)));
16071607

16081608
gossip_source.set_gossip_verifier(
16091609
Arc::clone(&chain_source),

src/liquidity.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
use std::collections::HashMap;
1111
use std::ops::Deref;
12-
use std::sync::{Arc, Mutex, RwLock};
12+
use std::sync::{Arc, Mutex, RwLock, Weak};
1313
use std::time::Duration;
1414

1515
use bitcoin::hashes::{sha256, Hash};
@@ -291,7 +291,7 @@ where
291291
lsps2_service: Option<LSPS2Service>,
292292
wallet: Arc<Wallet>,
293293
channel_manager: Arc<ChannelManager>,
294-
peer_manager: RwLock<Option<Arc<PeerManager>>>,
294+
peer_manager: RwLock<Option<Weak<PeerManager>>>,
295295
keys_manager: Arc<KeysManager>,
296296
liquidity_manager: Arc<LiquidityManager>,
297297
config: Arc<Config>,
@@ -302,7 +302,7 @@ impl<L: Deref> LiquiditySource<L>
302302
where
303303
L::Target: LdkLogger,
304304
{
305-
pub(crate) fn set_peer_manager(&self, peer_manager: Arc<PeerManager>) {
305+
pub(crate) fn set_peer_manager(&self, peer_manager: Weak<PeerManager>) {
306306
*self.peer_manager.write().unwrap() = Some(peer_manager);
307307
}
308308

@@ -715,8 +715,8 @@ where
715715
return;
716716
};
717717

718-
let init_features = if let Some(peer_manager) =
719-
self.peer_manager.read().unwrap().as_ref()
718+
let init_features = if let Some(Some(peer_manager)) =
719+
self.peer_manager.read().unwrap().as_ref().map(|weak| weak.upgrade())
720720
{
721721
// Fail if we're not connected to the prospective channel partner.
722722
if let Some(peer) = peer_manager.peer_by_node_id(&their_network_key) {

0 commit comments

Comments
 (0)