Skip to content

Commit eb83451

Browse files
committed
Add reenatrancy guard for process_pending_htlc_forwards
We add a reenatrancy guard to disallow entering `process_pending_htlc_forwards` multiple times. This makes sure that we'd skip any additional processing calls if a prior round/batch of processing is still underway.
1 parent 7758838 commit eb83451

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2720,6 +2720,10 @@ pub struct ChannelManager<
27202720
/// A simple atomic flag to ensure only one task at a time can be processing events asynchronously.
27212721
pending_events_processor: AtomicBool,
27222722

2723+
/// A simple atomic flag to ensure only one task at a time can be processing HTLC forwards via
2724+
/// [`Self::process_pending_htlc_forwards`].
2725+
pending_htlc_forwards_processor: AtomicBool,
2726+
27232727
/// If we are running during init (either directly during the deserialization method or in
27242728
/// block connection methods which run after deserialization but before normal operation) we
27252729
/// cannot provide the user with [`ChannelMonitorUpdate`]s through the normal update flow -
@@ -3784,6 +3788,7 @@ where
37843788

37853789
pending_events: Mutex::new(VecDeque::new()),
37863790
pending_events_processor: AtomicBool::new(false),
3791+
pending_htlc_forwards_processor: AtomicBool::new(false),
37873792
pending_background_events: Mutex::new(Vec::new()),
37883793
total_consistency_lock: RwLock::new(()),
37893794
background_events_processed_since_startup: AtomicBool::new(false),
@@ -6334,9 +6339,19 @@ where
63346339
///
63356340
/// Will regularly be called by the background processor.
63366341
pub fn process_pending_htlc_forwards(&self) {
6342+
if self
6343+
.pending_htlc_forwards_processor
6344+
.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
6345+
.is_err()
6346+
{
6347+
return;
6348+
}
6349+
63376350
let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
63386351
self.internal_process_pending_htlc_forwards()
63396352
});
6353+
6354+
self.pending_htlc_forwards_processor.store(false, Ordering::Release);
63406355
}
63416356

63426357
// Returns whether or not we need to re-persist.
@@ -16348,6 +16363,7 @@ where
1634816363

1634916364
pending_events: Mutex::new(pending_events_read),
1635016365
pending_events_processor: AtomicBool::new(false),
16366+
pending_htlc_forwards_processor: AtomicBool::new(false),
1635116367
pending_background_events: Mutex::new(pending_background_events),
1635216368
total_consistency_lock: RwLock::new(()),
1635316369
background_events_processed_since_startup: AtomicBool::new(false),

0 commit comments

Comments
 (0)