]> git.bitcoin.ninja Git - ldk-sample/commitdiff
Cleanly shut down PeerManager by disconnecting before we quit
authorMatt Corallo <git@bluematt.me>
Thu, 24 Mar 2022 00:05:31 +0000 (00:05 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 25 Mar 2022 23:21:49 +0000 (23:21 +0000)
This ensures we don't keep processing peer messages long after we
stopped the backgroundprocessor (which results in our
`ChannelManager` on disk being stale compared to monitors).

src/main.rs

index cdbb178a5f82b64fe3c60ec196176045137badef..e962db83a2556c11f81a44f0f4fbc892660ea963 100644 (file)
@@ -48,6 +48,7 @@ use std::io;
 use std::io::Write;
 use std::ops::Deref;
 use std::path::Path;
+use std::sync::atomic::{AtomicBool, Ordering};
 use std::sync::{Arc, Mutex};
 use std::time::{Duration, SystemTime};
 
@@ -528,6 +529,8 @@ async fn start_ldk() {
 
        let peer_manager_connection_handler = peer_manager.clone();
        let listening_port = args.ldk_peer_listening_port;
+       let stop_listen = Arc::new(AtomicBool::new(false));
+       let stop_listen_ref = Arc::clone(&stop_listen);
        tokio::spawn(async move {
                let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", listening_port))
                        .await
@@ -535,6 +538,9 @@ async fn start_ldk() {
                loop {
                        let peer_mgr = peer_manager_connection_handler.clone();
                        let tcp_stream = listener.accept().await.unwrap().0;
+                       if stop_listen_ref.load(Ordering::Acquire) {
+                               return;
+                       }
                        tokio::spawn(async move {
                                lightning_net_tokio::setup_inbound(
                                        peer_mgr.clone(),
@@ -704,6 +710,11 @@ async fn start_ldk() {
        )
        .await;
 
+       // Disconnect our peers and stop accepting new connections. This ensures we don't continue
+       // updating our channel data after we've stopped the background processor.
+       stop_listen.store(true, Ordering::Release);
+       peer_manager.disconnect_all_peers();
+
        // Stop the background processor.
        background_processor.stop().unwrap();
 }