Skip to content

Commit dff8088

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 7814af6 commit dff8088

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 -
@@ -3796,6 +3800,7 @@ where
37963800

37973801
pending_events: Mutex::new(VecDeque::new()),
37983802
pending_events_processor: AtomicBool::new(false),
3803+
pending_htlc_forwards_processor: AtomicBool::new(false),
37993804
pending_background_events: Mutex::new(Vec::new()),
38003805
total_consistency_lock: RwLock::new(()),
38013806
background_events_processed_since_startup: AtomicBool::new(false),
@@ -6326,9 +6331,19 @@ where
63266331
///
63276332
/// Will regularly be called by the background processor.
63286333
pub fn process_pending_htlc_forwards(&self) {
6334+
if self
6335+
.pending_htlc_forwards_processor
6336+
.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
6337+
.is_err()
6338+
{
6339+
return;
6340+
}
6341+
63296342
let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
63306343
self.internal_process_pending_htlc_forwards()
63316344
});
6345+
6346+
self.pending_htlc_forwards_processor.store(false, Ordering::Release);
63326347
}
63336348

63346349
// Returns whether or not we need to re-persist.
@@ -16459,6 +16474,7 @@ where
1645916474

1646016475
pending_events: Mutex::new(pending_events_read),
1646116476
pending_events_processor: AtomicBool::new(false),
16477+
pending_htlc_forwards_processor: AtomicBool::new(false),
1646216478
pending_background_events: Mutex::new(pending_background_events),
1646316479
total_consistency_lock: RwLock::new(()),
1646416480
background_events_processed_since_startup: AtomicBool::new(false),

0 commit comments

Comments
 (0)