From: Matt Corallo Date: Mon, 24 Apr 2023 03:48:42 +0000 (+0000) Subject: Check for `background-processor` exit condition before+after sleep X-Git-Tag: v0.0.115~4^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=05535913b3f1679298188745ab2dfc9e930b992e;p=rust-lightning Check for `background-processor` exit condition before+after sleep In a synchronous `BackgroundProcessor`, the exit is done by setting an atomic flag, which is most likely to happen while we're asleep. Thus, we previously checked for the exit condition after the sleep (and after we persisted the `ChannelManager`, if required, though this is no longer required and dates back to when we didn't do a re-persist after breaking out of the main loop). For an async `background-processor`, this is also fine, however because of the relatively longer sleep time, if the exit flag is set via a sleep check returning true during event processing, we may end up delaying exit rather substantially. In order to avoid this, we simply check for the exit condition both before and immediately after the sleep in `background-processor`. --- diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index af9fbcd15..c67066364 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -302,6 +302,12 @@ macro_rules! define_run_body { // persistence. $peer_manager.process_events(); + // Exit the loop if the background processor was requested to stop. + if $loop_exit_check { + log_trace!($logger, "Terminating background processor."); + break; + } + // We wait up to 100ms, but track how long it takes to detect being put to sleep, // see `await_start`'s use below. let mut await_start = None; @@ -309,16 +315,17 @@ macro_rules! define_run_body { let updates_available = $await; let await_slow = if $check_slow_await { $timer_elapsed(&mut await_start.unwrap(), 1) } else { false }; - if updates_available { - log_trace!($logger, "Persisting ChannelManager..."); - $persister.persist_manager(&*$channel_manager)?; - log_trace!($logger, "Done persisting ChannelManager."); - } // Exit the loop if the background processor was requested to stop. if $loop_exit_check { log_trace!($logger, "Terminating background processor."); break; } + + if updates_available { + log_trace!($logger, "Persisting ChannelManager..."); + $persister.persist_manager(&*$channel_manager)?; + log_trace!($logger, "Done persisting ChannelManager."); + } if $timer_elapsed(&mut last_freshness_call, FRESHNESS_TIMER) { log_trace!($logger, "Calling ChannelManager's timer_tick_occurred"); $channel_manager.timer_tick_occurred();