@@ -50,6 +50,8 @@ const FRESHNESS_TIMER: u64 = 60;
5050#[ cfg( test) ]
5151const FRESHNESS_TIMER : u64 = 1 ;
5252
53+ const PING_TIMER : u64 = 5 ;
54+
5355/// Trait which handles persisting a [`ChannelManager`] to disk.
5456///
5557/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
@@ -138,7 +140,8 @@ impl BackgroundProcessor {
138140 let stop_thread = Arc :: new ( AtomicBool :: new ( false ) ) ;
139141 let stop_thread_clone = stop_thread. clone ( ) ;
140142 let handle = thread:: spawn ( move || -> Result < ( ) , std:: io:: Error > {
141- let mut current_time = Instant :: now ( ) ;
143+ let mut last_freshness_call = Instant :: now ( ) ;
144+ let mut last_ping_call = Instant :: now ( ) ;
142145 loop {
143146 peer_manager. process_events ( ) ;
144147 channel_manager. process_pending_events ( & event_handler) ;
@@ -153,11 +156,27 @@ impl BackgroundProcessor {
153156 log_trace ! ( logger, "Terminating background processor." ) ;
154157 return Ok ( ( ) ) ;
155158 }
156- if current_time . elapsed ( ) . as_secs ( ) > FRESHNESS_TIMER {
157- log_trace ! ( logger, "Calling ChannelManager's and PeerManager's timer_tick_occurred" ) ;
159+ if last_freshness_call . elapsed ( ) . as_secs ( ) > FRESHNESS_TIMER {
160+ log_trace ! ( logger, "Calling ChannelManager's timer_tick_occurred" ) ;
158161 channel_manager. timer_tick_occurred ( ) ;
162+ last_freshness_call = Instant :: now ( ) ;
163+ }
164+ if last_ping_call. elapsed ( ) . as_secs ( ) > PING_TIMER * 2 {
165+ // On various platforms, we may be starved of CPU cycles for several reasons.
166+ // E.g. on iOS, if we've been in the background, we will be entirely paused.
167+ // Similarly, if we're on a desktop platform and the device has been asleep, we
168+ // may not get any cycles.
169+ // In any case, if we've been entirely paused for more than double our ping
170+ // timer, we should have disconnected all sockets by now (and they're probably
171+ // dead anyway), so disconnect them by calling `timer_tick_occurred()` twice.
172+ log_trace ! ( logger, "Awoke after more than double our ping timer, disconnecting peers." ) ;
173+ peer_manager. timer_tick_occurred ( ) ;
174+ peer_manager. timer_tick_occurred ( ) ;
175+ last_ping_call = Instant :: now ( ) ;
176+ } else if last_ping_call. elapsed ( ) . as_secs ( ) > PING_TIMER {
177+ log_trace ! ( logger, "Calling PeerManager's timer_tick_occurred" ) ;
159178 peer_manager. timer_tick_occurred ( ) ;
160- current_time = Instant :: now ( ) ;
179+ last_ping_call = Instant :: now ( ) ;
161180 }
162181 }
163182 } ) ;
@@ -441,8 +460,10 @@ mod tests {
441460 let bg_processor = BackgroundProcessor :: start ( persister, event_handler, nodes[ 0 ] . chain_monitor . clone ( ) , nodes[ 0 ] . node . clone ( ) , nodes[ 0 ] . peer_manager . clone ( ) , nodes[ 0 ] . logger . clone ( ) ) ;
442461 loop {
443462 let log_entries = nodes[ 0 ] . logger . lines . lock ( ) . unwrap ( ) ;
444- let desired_log = "Calling ChannelManager's and PeerManager's timer_tick_occurred" . to_string ( ) ;
445- if log_entries. get ( & ( "lightning_background_processor" . to_string ( ) , desired_log) ) . is_some ( ) {
463+ let desired_log = "Calling ChannelManager's timer_tick_occurred" . to_string ( ) ;
464+ let second_desired_log = "Calling PeerManager's timer_tick_occurred" . to_string ( ) ;
465+ if log_entries. get ( & ( "lightning_background_processor" . to_string ( ) , desired_log) ) . is_some ( ) &&
466+ log_entries. get ( & ( "lightning_background_processor" . to_string ( ) , second_desired_log) ) . is_some ( ) {
446467 break
447468 }
448469 }
0 commit comments