From aa40671f622e54eacd1525672aecd5a3b516384d Mon Sep 17 00:00:00 2001 From: benthecarman Date: Fri, 9 Oct 2026 13:06:58 -0500 Subject: [PATCH 1/3] 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 | 78 ++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 41 deletions(-) diff --git a/src/chain/bitcoind.rs b/src/chain/bitcoind.rs index 879a9f5743..8207383e04 100644 --- a/src/chain/bitcoind.rs +++ b/src/chain/bitcoind.rs @@ -361,15 +361,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! { @@ -381,41 +377,41 @@ 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(&offers_message_handler), - 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(&offers_message_handler), + 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); } } } From 7a2e99e56e85b0a045e315aad8e179d211032f4d Mon Sep 17 00:00:00 2001 From: benthecarman Date: Tue, 17 Nov 2026 11:05:20 -0600 Subject: [PATCH 2/3] fixup! Prevent slow chain polls from starving fee updates --- src/chain/bitcoind.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/chain/bitcoind.rs b/src/chain/bitcoind.rs index 8207383e04..2292cb13f2 100644 --- a/src/chain/bitcoind.rs +++ b/src/chain/bitcoind.rs @@ -51,6 +51,9 @@ use crate::{Error, PersistedNodeMetrics}; const CHAIN_POLLING_INTERVAL_SECS: u64 = 2; const CHAIN_POLLING_TIMEOUT_SECS: u64 = 10; +// Bitcoin Core's mempool-based fee estimation can change estimates between blocks, so we also +// refresh fee rate estimates periodically rather than only on new blocks. +const FEE_RATE_REFRESH_INTERVAL_SECS: u64 = 30; type BitcoindSpvClient = SpvClient, BitcoindClient>, Arc>; @@ -366,7 +369,9 @@ impl BitcoindChainSource { // 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 fee_rate_refresh_interval = Duration::from_secs(FEE_RATE_REFRESH_INTERVAL_SECS); let mut last_best_block_hash = None; + let mut last_fee_rate_update: Option = None; loop { tokio::select! { biased; @@ -399,7 +404,9 @@ impl BitcoindChainSource { } let current_best_block_hash = channel_manager.current_best_block().block_hash; - if last_best_block_hash != Some(current_best_block_hash) { + let fee_rate_refresh_due = last_fee_rate_update + .map_or(true, |last_update| last_update.elapsed() >= fee_rate_refresh_interval); + if last_best_block_hash != Some(current_best_block_hash) || fee_rate_refresh_due { tokio::select! { biased; _ = stop_sync_receiver.changed() => { @@ -412,6 +419,7 @@ impl BitcoindChainSource { update_res = self.update_fee_rate_estimates() => { if update_res.is_ok() { last_best_block_hash = Some(current_best_block_hash); + last_fee_rate_update = Some(Instant::now()); } } } From 3e7b18534d761b214055d65766271b5c565c1099 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Wed, 18 Nov 2026 12:15:29 -0600 Subject: [PATCH 3/3] fixup! Prevent slow chain polls from starving fee updates --- src/chain/bitcoind.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/chain/bitcoind.rs b/src/chain/bitcoind.rs index 2292cb13f2..cb90b4be6a 100644 --- a/src/chain/bitcoind.rs +++ b/src/chain/bitcoind.rs @@ -370,8 +370,9 @@ impl BitcoindChainSource { // for chain data, as using separate timers would allow chain polls taking longer than the // polling interval to starve the fee rate updates. let fee_rate_refresh_interval = Duration::from_secs(FEE_RATE_REFRESH_INTERVAL_SECS); - let mut last_best_block_hash = None; - let mut last_fee_rate_update: Option = None; + // We just updated fee rate estimates when initializing the wallets, so skip the initial update. + let mut last_best_block_hash = channel_manager.current_best_block().block_hash; + let mut last_fee_rate_update = Instant::now(); loop { tokio::select! { biased; @@ -404,9 +405,8 @@ impl BitcoindChainSource { } let current_best_block_hash = channel_manager.current_best_block().block_hash; - let fee_rate_refresh_due = last_fee_rate_update - .map_or(true, |last_update| last_update.elapsed() >= fee_rate_refresh_interval); - if last_best_block_hash != Some(current_best_block_hash) || fee_rate_refresh_due { + let fee_rate_refresh_due = last_fee_rate_update.elapsed() >= fee_rate_refresh_interval; + if last_best_block_hash != current_best_block_hash || fee_rate_refresh_due { tokio::select! { biased; _ = stop_sync_receiver.changed() => { @@ -418,8 +418,8 @@ impl BitcoindChainSource { } update_res = self.update_fee_rate_estimates() => { if update_res.is_ok() { - last_best_block_hash = Some(current_best_block_hash); - last_fee_rate_update = Some(Instant::now()); + last_best_block_hash = current_best_block_hash; + last_fee_rate_update = Instant::now(); } } }