]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Check for `background-processor` exit condition before+after sleep 2023-04-bp-exit-fast
authorMatt Corallo <git@bluematt.me>
Mon, 24 Apr 2023 03:48:42 +0000 (03:48 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 24 Apr 2023 03:49:48 +0000 (03:49 +0000)
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`.

lightning-background-processor/src/lib.rs

index af9fbcd15f5114a985025fa588a58d2e8e6fc3f4..c6706636433e2862380b7719237a1d1b0465cb72 100644 (file)
@@ -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();