Skip to content

Commit 508c202

Browse files
Detect if the background processor exits early and shutdown if so
1 parent b7daadc commit 508c202

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/main.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ async fn start_ldk() {
802802

803803
// Step 20: Background Processing
804804
let (bp_exit, bp_exit_check) = tokio::sync::watch::channel(());
805-
let background_processor = tokio::spawn(process_events_async(
805+
let mut background_processor = tokio::spawn(process_events_async(
806806
Arc::clone(&persister),
807807
event_handler,
808808
chain_monitor.clone(),
@@ -898,7 +898,7 @@ async fn start_ldk() {
898898
));
899899

900900
// Start the CLI.
901-
cli::poll_for_user_input(
901+
let cli_poll = tokio::spawn(cli::poll_for_user_input(
902902
Arc::clone(&peer_manager),
903903
Arc::clone(&channel_manager),
904904
Arc::clone(&keys_manager),
@@ -910,17 +910,29 @@ async fn start_ldk() {
910910
network,
911911
Arc::clone(&logger),
912912
Arc::clone(&persister),
913-
)
914-
.await;
913+
));
914+
915+
// Exit if either CLI polling exits or the background processor exits (which shouldn't happen
916+
// unless we fail to write to the filesystem).
917+
tokio::select! {
918+
_ = cli_poll => {},
919+
bg_res = &mut background_processor => {
920+
stop_listen_connect.store(true, Ordering::Release);
921+
peer_manager.disconnect_all_peers();
922+
panic!("ERR: background processing stopped with result {:?}, exiting", bg_res);
923+
},
924+
}
915925

916926
// Disconnect our peers and stop accepting new connections. This ensures we don't continue
917927
// updating our channel data after we've stopped the background processor.
918928
stop_listen_connect.store(true, Ordering::Release);
919929
peer_manager.disconnect_all_peers();
920930

921931
// Stop the background processor.
922-
bp_exit.send(()).unwrap();
923-
background_processor.await.unwrap().unwrap();
932+
if !bp_exit.is_closed() {
933+
bp_exit.send(()).unwrap();
934+
background_processor.await.unwrap().unwrap();
935+
}
924936
}
925937

926938
#[tokio::main]

0 commit comments

Comments
 (0)