@@ -41,9 +41,7 @@ use std::ops::Deref;
41
41
/// for unilateral chain closure fees are at risk.
42
42
pub struct BackgroundProcessor {
43
43
stop_thread : Arc < AtomicBool > ,
44
- /// May be used to retrieve and handle the error if `BackgroundProcessor`'s thread
45
- /// exits due to an error while persisting.
46
- pub thread_handle : Option < JoinHandle < Result < ( ) , std:: io:: Error > > > ,
44
+ thread_handle : Option < JoinHandle < Result < ( ) , std:: io:: Error > > > ,
47
45
}
48
46
49
47
#[ cfg( not( test) ) ]
@@ -84,21 +82,25 @@ ChannelManagerPersister<Signer, M, T, K, F, L> for Fun where
84
82
}
85
83
86
84
impl BackgroundProcessor {
87
- /// Start a background thread that takes care of responsibilities enumerated in the top-level
88
- /// documentation.
85
+ /// Start a background thread that takes care of responsibilities enumerated in the [ top-level
86
+ /// documentation] .
89
87
///
90
- /// If `persist_manager` returns an error, then this thread will return said error (and
91
- /// `start()` will need to be called again to restart the `BackgroundProcessor`). Users should
92
- /// wait on [`thread_handle`]'s `join()` method to be able to tell if and when an error is
93
- /// returned, or implement `persist_manager` such that an error is never returned to the
94
- /// `BackgroundProcessor`
88
+ /// The thread runs indefinitely unless the object is dropped, [`stop`] is called, or
89
+ /// `persist_manager` returns an error. In case of an error, the error is retrieved by calling
90
+ /// either [`join`] or [`stop`].
91
+ ///
92
+ /// Typically, users should either implement [`ChannelManagerPersister`] to never return an
93
+ /// error or call [`join`] and handle any error that may arise. For the latter case, the
94
+ /// `BackgroundProcessor` must be restarted by calling `start` again after handling the error.
95
95
///
96
96
/// `persist_manager` is responsible for writing out the [`ChannelManager`] to disk, and/or
97
97
/// uploading to one or more backup services. See [`ChannelManager::write`] for writing out a
98
98
/// [`ChannelManager`]. See [`FilesystemPersister::persist_manager`] for Rust-Lightning's
99
99
/// provided implementation.
100
100
///
101
- /// [`thread_handle`]: BackgroundProcessor::thread_handle
101
+ /// [top-level documentation]: Self
102
+ /// [`join`]: Self::join
103
+ /// [`stop`]: Self::stop
102
104
/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
103
105
/// [`ChannelManager::write`]: lightning::ln::channelmanager::ChannelManager#impl-Writeable
104
106
/// [`FilesystemPersister::persist_manager`]: lightning_persister::FilesystemPersister::persist_manager
@@ -161,14 +163,30 @@ impl BackgroundProcessor {
161
163
Self { stop_thread : stop_thread_clone, thread_handle : Some ( handle) }
162
164
}
163
165
164
- /// Stop `BackgroundProcessor`'s thread.
166
+ /// Join `BackgroundProcessor`'s thread, returning any error that occurred while persisting
167
+ /// [`ChannelManager`].
168
+ ///
169
+ /// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
170
+ pub fn join ( mut self ) -> Result < ( ) , std:: io:: Error > {
171
+ assert ! ( self . thread_handle. is_some( ) ) ;
172
+ self . join_thread ( )
173
+ }
174
+
175
+ /// Stop `BackgroundProcessor`'s thread, returning any error that occurred while persisting
176
+ /// [`ChannelManager`].
177
+ ///
178
+ /// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
165
179
pub fn stop ( mut self ) -> Result < ( ) , std:: io:: Error > {
166
180
assert ! ( self . thread_handle. is_some( ) ) ;
167
181
self . stop_and_join_thread ( )
168
182
}
169
183
170
184
fn stop_and_join_thread ( & mut self ) -> Result < ( ) , std:: io:: Error > {
171
185
self . stop_thread . store ( true , Ordering :: Release ) ;
186
+ self . join_thread ( )
187
+ }
188
+
189
+ fn join_thread ( & mut self ) -> Result < ( ) , std:: io:: Error > {
172
190
match self . thread_handle . take ( ) {
173
191
Some ( handle) => handle. join ( ) . unwrap ( ) ,
174
192
None => Ok ( ( ) ) ,
@@ -430,7 +448,7 @@ mod tests {
430
448
let persister = |_: & _ | Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , "test" ) ) ;
431
449
let event_handler = |_| { } ;
432
450
let bg_processor = BackgroundProcessor :: start ( persister, event_handler, nodes[ 0 ] . chain_monitor . clone ( ) , nodes[ 0 ] . node . clone ( ) , nodes[ 0 ] . peer_manager . clone ( ) , nodes[ 0 ] . logger . clone ( ) ) ;
433
- match bg_processor. stop ( ) {
451
+ match bg_processor. join ( ) {
434
452
Ok ( _) => panic ! ( "Expected error persisting manager" ) ,
435
453
Err ( e) => {
436
454
assert_eq ! ( e. kind( ) , std:: io:: ErrorKind :: Other ) ;
0 commit comments