@@ -43,7 +43,7 @@ pub struct BackgroundProcessor {
4343 stop_thread : Arc < AtomicBool > ,
4444 /// May be used to retrieve and handle the error if `BackgroundProcessor`'s thread
4545 /// exits due to an error while persisting.
46- pub thread_handle : Option < JoinHandle < Result < ( ) , std:: io:: Error > > > ,
46+ thread_handle : Option < JoinHandle < Result < ( ) , std:: io:: Error > > > ,
4747}
4848
4949#[ cfg( not( test) ) ]
@@ -84,21 +84,25 @@ ChannelManagerPersister<Signer, M, T, K, F, L> for Fun where
8484}
8585
8686impl BackgroundProcessor {
87- /// Start a background thread that takes care of responsibilities enumerated in the top-level
88- /// documentation.
87+ /// Start a background thread that takes care of responsibilities enumerated in the [ top-level
88+ /// documentation] .
8989 ///
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`
90+ /// The thread runs indefinitely unless the object is dropped, [`stop`] is called, or
91+ /// `persist_manager` returns an error. In case of an error, the error is retrieved by calling
92+ /// either [`join`] or [`stop`].
93+ ///
94+ /// Typically, users should either implement [`ChannelManagerPersister`] to never return an
95+ /// error or call [`join`] and handle any error that may arise. For the latter case, the
96+ /// `BackgroundProcessor` must be restarted by calling `start` again after handling the error.
9597 ///
9698 /// `persist_manager` is responsible for writing out the [`ChannelManager`] to disk, and/or
9799 /// uploading to one or more backup services. See [`ChannelManager::write`] for writing out a
98100 /// [`ChannelManager`]. See [`FilesystemPersister::persist_manager`] for Rust-Lightning's
99101 /// provided implementation.
100102 ///
101- /// [`thread_handle`]: BackgroundProcessor::thread_handle
103+ /// [top-level documentation]: Self
104+ /// [`join`]: Self::join
105+ /// [`stop`]: Self::stop
102106 /// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
103107 /// [`ChannelManager::write`]: lightning::ln::channelmanager::ChannelManager#impl-Writeable
104108 /// [`FilesystemPersister::persist_manager`]: lightning_persister::FilesystemPersister::persist_manager
@@ -161,14 +165,30 @@ impl BackgroundProcessor {
161165 Self { stop_thread : stop_thread_clone, thread_handle : Some ( handle) }
162166 }
163167
164- /// Stop `BackgroundProcessor`'s thread.
168+ /// Join `BackgroundProcessor`'s thread, returning any error that occurred while persisting
169+ /// [`ChannelManager`].
170+ ///
171+ /// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
172+ pub fn join ( mut self ) -> Result < ( ) , std:: io:: Error > {
173+ assert ! ( self . thread_handle. is_some( ) ) ;
174+ self . join_thread ( )
175+ }
176+
177+ /// Stop `BackgroundProcessor`'s thread, returning any error that occurred while persisting
178+ /// [`ChannelManager`].
179+ ///
180+ /// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
165181 pub fn stop ( mut self ) -> Result < ( ) , std:: io:: Error > {
166182 assert ! ( self . thread_handle. is_some( ) ) ;
167183 self . stop_and_join_thread ( )
168184 }
169185
170186 fn stop_and_join_thread ( & mut self ) -> Result < ( ) , std:: io:: Error > {
171187 self . stop_thread . store ( true , Ordering :: Release ) ;
188+ self . join_thread ( )
189+ }
190+
191+ fn join_thread ( & mut self ) -> Result < ( ) , std:: io:: Error > {
172192 match self . thread_handle . take ( ) {
173193 Some ( handle) => handle. join ( ) . unwrap ( ) ,
174194 None => Ok ( ( ) ) ,
@@ -430,7 +450,7 @@ mod tests {
430450 let persister = |_: & _ | Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , "test" ) ) ;
431451 let event_handler = |_| { } ;
432452 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 ( ) {
453+ match bg_processor. join ( ) {
434454 Ok ( _) => panic ! ( "Expected error persisting manager" ) ,
435455 Err ( e) => {
436456 assert_eq ! ( e. kind( ) , std:: io:: ErrorKind :: Other ) ;
0 commit comments