From 248a107e3df81172a3df5304005bba9360df8432 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Wed, 19 May 2021 12:26:55 -0700 Subject: [PATCH] Guard get_and_clear_pending_msg_events --- lightning/src/ln/channelmanager.rs | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 592ab2b6c..e39a778bc 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -62,6 +62,7 @@ use util::logger::Logger; use util::errors::APIError; use core::{cmp, mem}; +use std::cell::RefCell; use std::collections::{HashMap, hash_map, HashSet}; use std::io::{Cursor, Read}; use std::sync::{Arc, Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard}; @@ -3692,16 +3693,29 @@ impl MessageSend L::Target: Logger, { fn get_and_clear_pending_msg_events(&self) -> Vec { - //TODO: This behavior should be documented. It's non-intuitive that we query - // ChannelMonitors when clearing other events. - self.process_pending_monitor_events(); + let events = RefCell::new(Vec::new()); + PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock, &self.persistence_notifier, || { + let mut result = NotifyOption::SkipPersist; - self.check_free_holding_cells(); + // TODO: This behavior should be documented. It's unintuitive that we query + // ChannelMonitors when clearing other events. + if self.process_pending_monitor_events() { + result = NotifyOption::DoPersist; + } - let mut ret = Vec::new(); - let mut channel_state = self.channel_state.lock().unwrap(); - mem::swap(&mut ret, &mut channel_state.pending_msg_events); - ret + self.check_free_holding_cells(); + + let mut pending_events = Vec::new(); + let mut channel_state = self.channel_state.lock().unwrap(); + mem::swap(&mut pending_events, &mut channel_state.pending_msg_events); + + if !pending_events.is_empty() { + events.replace(pending_events); + } + + result + }); + events.into_inner() } } -- 2.39.5