Skip to content

Commit 0553591

Browse files
committed
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`.
1 parent bc54441 commit 0553591

File tree

1 file changed

+12
-5
lines changed
  • lightning-background-processor/src

1 file changed

+12
-5
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,23 +302,30 @@ macro_rules! define_run_body {
302302
// persistence.
303303
$peer_manager.process_events();
304304

305+
// Exit the loop if the background processor was requested to stop.
306+
if $loop_exit_check {
307+
log_trace!($logger, "Terminating background processor.");
308+
break;
309+
}
310+
305311
// We wait up to 100ms, but track how long it takes to detect being put to sleep,
306312
// see `await_start`'s use below.
307313
let mut await_start = None;
308314
if $check_slow_await { await_start = Some($get_timer(1)); }
309315
let updates_available = $await;
310316
let await_slow = if $check_slow_await { $timer_elapsed(&mut await_start.unwrap(), 1) } else { false };
311317

312-
if updates_available {
313-
log_trace!($logger, "Persisting ChannelManager...");
314-
$persister.persist_manager(&*$channel_manager)?;
315-
log_trace!($logger, "Done persisting ChannelManager.");
316-
}
317318
// Exit the loop if the background processor was requested to stop.
318319
if $loop_exit_check {
319320
log_trace!($logger, "Terminating background processor.");
320321
break;
321322
}
323+
324+
if updates_available {
325+
log_trace!($logger, "Persisting ChannelManager...");
326+
$persister.persist_manager(&*$channel_manager)?;
327+
log_trace!($logger, "Done persisting ChannelManager.");
328+
}
322329
if $timer_elapsed(&mut last_freshness_call, FRESHNESS_TIMER) {
323330
log_trace!($logger, "Calling ChannelManager's timer_tick_occurred");
324331
$channel_manager.timer_tick_occurred();

0 commit comments

Comments
 (0)