Skip to content

Commit ce7e7d3

Browse files
committed
Clarify and consolidate event handling requirements
We've seen a bit of user confusion about the requirements for event handling, largely because the idempotency and consistency requirements weren't super clearly phrased. While we're at it, we also consolidate some documentation out of the event handling function onto the trait itself. Fixes #1675.
1 parent 8c6cb99 commit ce7e7d3

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

lightning/src/ln/channelmanager.rs

-4
Original file line numberDiff line numberDiff line change
@@ -5671,10 +5671,6 @@ where
56715671
///
56725672
/// An [`EventHandler`] may safely call back to the provider in order to handle an event.
56735673
/// However, it must not call [`Writeable::write`] as doing so would result in a deadlock.
5674-
///
5675-
/// Pending events are persisted as part of [`ChannelManager`]. While these events are cleared
5676-
/// when processed, an [`EventHandler`] must be able to handle previously seen events when
5677-
/// restarting from an old state.
56785674
fn process_pending_events<H: Deref>(&self, handler: H) where H::Target: EventHandler {
56795675
PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock, &self.persistence_notifier, || {
56805676
let mut result = NotifyOption::SkipPersist;

lightning/src/util/events.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1207,11 +1207,17 @@ pub trait OnionMessageProvider {
12071207
///
12081208
/// # Requirements
12091209
///
1210-
/// See [`process_pending_events`] for requirements around event processing.
1211-
///
12121210
/// When using this trait, [`process_pending_events`] will call [`handle_event`] for each pending
1213-
/// event since the last invocation. The handler must either act upon the event immediately
1214-
/// or preserve it for later handling.
1211+
/// event since the last invocation.
1212+
///
1213+
/// In order to ensure no [`Event`]s are lost, implementors of this trait will persist [`Event`]s
1214+
/// and replay any unhandled events on startup. An [`Event`] is considered handled when
1215+
/// [`process_pending_events`] returns, thus handlers MUST fully handle [`Event`]s and persist any
1216+
/// relevant changes to disk *before* returning.
1217+
///
1218+
/// Further, because an application may crash between an [`Event`] being handled and the
1219+
/// implementor of this trait being re-serialized, [`Event`] handling must be idempotent - in
1220+
/// effect, [`Event`]s may be replayed.
12151221
///
12161222
/// Note, handlers may call back into the provider and thus deadlocking must be avoided. Be sure to
12171223
/// consult the provider's documentation on the implication of processing events and how a handler
@@ -1228,9 +1234,7 @@ pub trait OnionMessageProvider {
12281234
pub trait EventsProvider {
12291235
/// Processes any events generated since the last call using the given event handler.
12301236
///
1231-
/// Subsequent calls must only process new events. However, handlers must be capable of handling
1232-
/// duplicate events across process restarts. This may occur if the provider was recovered from
1233-
/// an old state (i.e., it hadn't been successfully persisted after processing pending events).
1237+
/// See the trait-level documentation for requirements.
12341238
fn process_pending_events<H: Deref>(&self, handler: H) where H::Target: EventHandler;
12351239
}
12361240

0 commit comments

Comments
 (0)