From 418dc30a28184c0be9eb5bb8ac5eedf4eb0b8d45 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Fri, 9 Oct 2026 13:06:58 -0500 Subject: [PATCH] Prevent slow chain polls from starving fee updates The bitcoind polling loop used a biased select over two separate two-second intervals, with the chain polling tick prioritized over the fee rate update tick. If each chain poll took longer than the polling interval, the chain tick was already ready every time we returned to the select, so the fee rate update branch was never taken and fee estimates could go stale indefinitely. Since both intervals fire at the same rate anyway, we now drop the separate fee rate interval and update fee rate estimates right after each chain poll whenever the best block changed. Every step still races against the stop signal, so shutdown remains prompt. This was found by Project Loupe. This change was made with the help of Claude Code. --- src/chain/bitcoind.rs | 76 ++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 40 deletions(-) diff --git a/src/chain/bitcoind.rs b/src/chain/bitcoind.rs index 9ed38c2128..085c1091e6 100644 --- a/src/chain/bitcoind.rs +++ b/src/chain/bitcoind.rs @@ -334,15 +334,11 @@ impl BitcoindChainSource { tokio::time::interval(Duration::from_secs(CHAIN_POLLING_INTERVAL_SECS)); chain_polling_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); - let mut fee_rate_update_interval = - tokio::time::interval(Duration::from_secs(CHAIN_POLLING_INTERVAL_SECS)); - // When starting up, we just blocked on updating, so skip the first tick. - fee_rate_update_interval.reset(); - fee_rate_update_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); - log_info!(self.logger, "Starting continuous polling for chain updates."); - // Start the polling loop. + // Start the polling loop. We update fee rate estimates on the same tick right after polling + // for chain data, as using separate timers would allow chain polls taking longer than the + // polling interval to starve the fee rate updates. let mut last_best_block_hash = None; loop { tokio::select! { @@ -354,40 +350,40 @@ impl BitcoindChainSource { ); return; } - _ = chain_polling_interval.tick() => { - tokio::select! { - biased; - _ = stop_sync_receiver.changed() => { - log_trace!( - self.logger, - "Stopping polling for new chain data.", - ); - return; - } - _ = self.poll_and_update_listeners( - Arc::clone(&onchain_wallet), - Arc::clone(&channel_manager), - Arc::clone(&chain_monitor), - Arc::clone(&output_sweeper) - ) => {} - } + _ = chain_polling_interval.tick() => {} + } + + tokio::select! { + biased; + _ = stop_sync_receiver.changed() => { + log_trace!( + self.logger, + "Stopping polling for new chain data.", + ); + return; } - _ = fee_rate_update_interval.tick() => { - if last_best_block_hash != Some(channel_manager.current_best_block().block_hash) { - tokio::select! { - biased; - _ = stop_sync_receiver.changed() => { - log_trace!( - self.logger, - "Stopping polling for new chain data.", - ); - return; - } - update_res = self.update_fee_rate_estimates() => { - if update_res.is_ok() { - last_best_block_hash = Some(channel_manager.current_best_block().block_hash); - } - } + _ = self.poll_and_update_listeners( + Arc::clone(&onchain_wallet), + Arc::clone(&channel_manager), + Arc::clone(&chain_monitor), + Arc::clone(&output_sweeper) + ) => {} + } + + let current_best_block_hash = channel_manager.current_best_block().block_hash; + if last_best_block_hash != Some(current_best_block_hash) { + tokio::select! { + biased; + _ = stop_sync_receiver.changed() => { + log_trace!( + self.logger, + "Stopping polling for new chain data.", + ); + return; + } + update_res = self.update_fee_rate_estimates() => { + if update_res.is_ok() { + last_best_block_hash = Some(current_best_block_hash); } } }