Release write lock between monitor update iterations.

Previously, updating block data on a chain monitor
would acquire a write lock on all of its associated
channel monitors and not release it until the loop
completed.

Now, we instead acquire it on each iteration,
fixing #2470.
This commit is contained in:
Arik Sosman 2023-08-25 12:31:33 -07:00
parent 3dffe54258
commit c7a4949a25
No known key found for this signature in database
GPG key ID: F4FB5A3366C4D92E

View file

@ -42,6 +42,7 @@ use crate::ln::channelmanager::ChannelDetails;
use crate::prelude::*; use crate::prelude::*;
use crate::sync::{RwLock, RwLockReadGuard, Mutex, MutexGuard}; use crate::sync::{RwLock, RwLockReadGuard, Mutex, MutexGuard};
use core::iter::FromIterator;
use core::ops::Deref; use core::ops::Deref;
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use bitcoin::secp256k1::PublicKey; use bitcoin::secp256k1::PublicKey;
@ -285,7 +286,22 @@ where C::Target: chain::Filter,
where where
FN: Fn(&ChannelMonitor<ChannelSigner>, &TransactionData) -> Vec<TransactionOutputs> FN: Fn(&ChannelMonitor<ChannelSigner>, &TransactionData) -> Vec<TransactionOutputs>
{ {
let funding_outpoints: HashSet<OutPoint> = HashSet::from_iter(self.monitors.read().unwrap().keys().cloned());
for funding_outpoint in funding_outpoints.iter() {
let monitor_lock = self.monitors.read().unwrap();
if let Some(monitor_state) = monitor_lock.get(funding_outpoint) {
self.update_monitor_with_chain_data(header, best_height, txdata, &process, funding_outpoint, &monitor_state);
}
}
// do some followup cleanup if any funding outpoints were added in between iterations
let monitor_states = self.monitors.write().unwrap(); let monitor_states = self.monitors.write().unwrap();
for (funding_outpoint, monitor_state) in monitor_states.iter() {
if !funding_outpoints.contains(funding_outpoint) {
self.update_monitor_with_chain_data(header, best_height, txdata, &process, funding_outpoint, &monitor_state);
}
}
if let Some(height) = best_height { if let Some(height) = best_height {
// If the best block height is being updated, update highest_chain_height under the // If the best block height is being updated, update highest_chain_height under the
// monitors write lock. // monitors write lock.
@ -295,8 +311,9 @@ where C::Target: chain::Filter,
self.highest_chain_height.store(new_height, Ordering::Release); self.highest_chain_height.store(new_height, Ordering::Release);
} }
} }
}
for (funding_outpoint, monitor_state) in monitor_states.iter() { fn update_monitor_with_chain_data<FN>(&self, header: &BlockHeader, best_height: Option<u32>, txdata: &TransactionData, process: FN, funding_outpoint: &OutPoint, monitor_state: &MonitorHolder<ChannelSigner>) where FN: Fn(&ChannelMonitor<ChannelSigner>, &TransactionData) -> Vec<TransactionOutputs> {
let monitor = &monitor_state.monitor; let monitor = &monitor_state.monitor;
let mut txn_outputs; let mut txn_outputs;
{ {
@ -322,11 +339,11 @@ where C::Target: chain::Filter,
monitor_state.channel_perm_failed.store(true, Ordering::Release); monitor_state.channel_perm_failed.store(true, Ordering::Release);
self.pending_monitor_events.lock().unwrap().push((*funding_outpoint, vec![MonitorEvent::UpdateFailed(*funding_outpoint)], monitor.get_counterparty_node_id())); self.pending_monitor_events.lock().unwrap().push((*funding_outpoint, vec![MonitorEvent::UpdateFailed(*funding_outpoint)], monitor.get_counterparty_node_id()));
self.event_notifier.notify(); self.event_notifier.notify();
}, }
ChannelMonitorUpdateStatus::InProgress => { ChannelMonitorUpdateStatus::InProgress => {
log_debug!(self.logger, "Channel Monitor sync for channel {} in progress, holding events until completion!", log_funding_info!(monitor)); log_debug!(self.logger, "Channel Monitor sync for channel {} in progress, holding events until completion!", log_funding_info!(monitor));
pending_monitor_updates.push(update_id); pending_monitor_updates.push(update_id);
}, }
} }
} }
@ -347,7 +364,6 @@ where C::Target: chain::Filter,
} }
} }
} }
}
/// Creates a new `ChainMonitor` used to watch on-chain activity pertaining to channels. /// Creates a new `ChainMonitor` used to watch on-chain activity pertaining to channels.
/// ///