]> git.bitcoin.ninja Git - ldk-sample/commitdiff
Attempt a last-ditch ChannelManager persistence if the BP exits 2023-08-last-ditch-write
authorMatt Corallo <git@bluematt.me>
Sun, 13 Aug 2023 22:40:25 +0000 (22:40 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 14 Aug 2023 18:48:05 +0000 (18:48 +0000)
If the BP exits because it failed to write our ChannelManager to
disk, we'll probably lose some channels on startup. However,
there's still some chance for us to write the `ChannelManager` in a
retry, which we should do before we exit.

src/main.rs

index 938df1232390ac589a20a0d2425fcaeb2d8c3480..3f01cfe26a2d35ef49cd456c634c282a8af152a9 100644 (file)
@@ -914,12 +914,11 @@ async fn start_ldk() {
 
        // Exit if either CLI polling exits or the background processor exits (which shouldn't happen
        // unless we fail to write to the filesystem).
+       let mut bg_res = Ok(Ok(()));
        tokio::select! {
                _ = cli_poll => {},
-               bg_res = &mut background_processor => {
-                       stop_listen_connect.store(true, Ordering::Release);
-                       peer_manager.disconnect_all_peers();
-                       panic!("ERR: background processing stopped with result {:?}, exiting", bg_res);
+               bg_exit = &mut background_processor => {
+                       bg_res = bg_exit;
                },
        }
 
@@ -928,6 +927,21 @@ async fn start_ldk() {
        stop_listen_connect.store(true, Ordering::Release);
        peer_manager.disconnect_all_peers();
 
+       if let Err(e) = bg_res {
+               let persist_res = persister.persist("manager", &*channel_manager).unwrap();
+               use lightning::util::logger::Logger;
+               lightning::log_error!(
+                       &*logger,
+                       "Last-ditch ChannelManager persistence result: {:?}",
+                       persist_res
+               );
+               panic!(
+                       "ERR: background processing stopped with result {:?}, exiting.\n\
+                       Last-ditch ChannelManager persistence result {:?}",
+                       e, persist_res
+               );
+       }
+
        // Stop the background processor.
        if !bp_exit.is_closed() {
                bp_exit.send(()).unwrap();