Skip to content

Commit 4a3d8e5

Browse files
committed
node: Prevent leak of LxTasks
1 parent 05a5b0e commit 4a3d8e5

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

node/src/init.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ async fn spawn_p2p_listener(
632632
info!("Listening for LN P2P connections on port {}", peer_port);
633633

634634
let handle = LxTask::spawn(async move {
635-
let mut child_tasks = Vec::with_capacity(1);
635+
let mut child_tasks = FuturesUnordered::new();
636636

637637
loop {
638638
tokio::select! {
@@ -670,15 +670,23 @@ async fn spawn_p2p_listener(
670670

671671
child_tasks.push(child_task);
672672
}
673+
// To prevent a memory leak of LxTasks, we select! on the
674+
// futures unordered so that we can clear out LxTasks for peers
675+
// that disconnect before the node shuts down.
676+
Some(join_res) = child_tasks.next() => {
677+
if let Err(e) = join_res {
678+
error!("P2P connection task panicked: {e:#}");
679+
}
680+
}
673681
_ = shutdown.recv() =>
674682
break info!("LN P2P listen task shutting down"),
675683
}
676684
}
677685

678686
// Wait on all child tasks to finish (i.e. all connections close).
679-
for res in future::join_all(child_tasks).await {
680-
if let Err(e) = res {
681-
error!("P2P task panicked: {:#}", e);
687+
while let Some(join_res) = child_tasks.next().await {
688+
if let Err(e) = join_res {
689+
error!("P2P connection task panicked: {:#}", e);
682690
}
683691
}
684692

0 commit comments

Comments
 (0)