File tree Expand file tree Collapse file tree 1 file changed +12
-4
lines changed Expand file tree Collapse file tree 1 file changed +12
-4
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments