Skip to content

Commit 0f1a3b1

Browse files
committed
Handle being asleep for more than double our ping time gracefully
If we've been asleep for double our ping time, for whatever reason, disconnect all open sockets.
1 parent a00eec1 commit 0f1a3b1

File tree

1 file changed

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

1 file changed

+13
-1
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,19 @@ impl BackgroundProcessor {
160160
channel_manager.timer_tick_occurred();
161161
last_freshness_call = Instant::now();
162162
}
163-
if last_ping_call.elapsed().as_secs() > PING_TIMER {
163+
if last_ping_call.elapsed().as_secs() > PING_TIMER * 2 {
164+
// On various platforms, we may be starved of CPU cycles for several reasons.
165+
// E.g. on iOS, if we've been in the background, we will be entirely paused.
166+
// Similarly, if we're on a desktop platform and the device has been asleep, we
167+
// may not get any cycles.
168+
// In any case, if we've been entirely paused for more than double our ping
169+
// timer, we should have disconnected all sockets by now (and they're probably
170+
// dead anyway), so disconnect them by calling `timer_tick_occurred()` twice.
171+
log_trace!(logger, "Awoke after more than double our ping timer, disconnecting peers.");
172+
peer_manager.timer_tick_occurred();
173+
peer_manager.timer_tick_occurred();
174+
last_ping_call = Instant::now();
175+
} else if last_ping_call.elapsed().as_secs() > PING_TIMER {
164176
log_trace!(logger, "Calling PeerManager's timer_tick_occurred");
165177
peer_manager.timer_tick_occurred();
166178
last_ping_call = Instant::now();

0 commit comments

Comments
 (0)