Skip to content

Commit fb043e8

Browse files
committed
Avoid disconnecting all peers if user event processing is slow
In the sample client (and likely other downstream users), event processing may block on slow operations (e.g. Bitcoin Core RPCs). This should be fine, except that we consider this a case of possible backgrounding and disconnect all of our peers when it happens. Instead, we here avoid considering event processing time in the time between PeerManager events.
1 parent 08c99e5 commit fb043e8

File tree

1 file changed

+8
-1
lines changed
  • lightning-background-processor/src

1 file changed

+8
-1
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,10 @@ impl BackgroundProcessor {
209209
let mut last_ping_call = Instant::now();
210210
loop {
211211
peer_manager.process_events();
212+
let user_ev_handle_start = Instant::now();
212213
channel_manager.process_pending_events(&event_handler);
213214
chain_monitor.process_pending_events(&event_handler);
215+
let user_ev_handle_time = Instant::now() - user_ev_handle_start;
214216
let updates_available =
215217
channel_manager.await_persistable_update_timeout(Duration::from_millis(100));
216218
if updates_available {
@@ -226,14 +228,19 @@ impl BackgroundProcessor {
226228
channel_manager.timer_tick_occurred();
227229
last_freshness_call = Instant::now();
228230
}
229-
if last_ping_call.elapsed().as_secs() > PING_TIMER * 2 {
231+
if last_ping_call.elapsed().as_secs() - user_ev_handle_time > PING_TIMER * 2 {
230232
// On various platforms, we may be starved of CPU cycles for several reasons.
231233
// E.g. on iOS, if we've been in the background, we will be entirely paused.
232234
// Similarly, if we're on a desktop platform and the device has been asleep, we
233235
// may not get any cycles.
234236
// In any case, if we've been entirely paused for more than double our ping
235237
// timer, we should have disconnected all sockets by now (and they're probably
236238
// dead anyway), so disconnect them by calling `timer_tick_occurred()` twice.
239+
// Note that we have to take care to not get here just because user event
240+
// processing was slow at the top of the loop. For example, the sample client
241+
// may call Bitcoin Core RPCs during event handling, which very often takes
242+
// more than a handful of seconds to complete, and shouldn't disconnect all our
243+
// peers.
237244
log_trace!(logger, "Awoke after more than double our ping timer, disconnecting peers.");
238245
peer_manager.timer_tick_occurred();
239246
peer_manager.timer_tick_occurred();

0 commit comments

Comments
 (0)