Skip to content

Commit d9459e0

Browse files
f matt tweaks
1 parent 6639c3b commit d9459e0

File tree

2 files changed

+15
-38
lines changed

2 files changed

+15
-38
lines changed

background-processor/src/lib.rs

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
#[macro_use] extern crate lightning;
2+
13
use lightning::chain;
24
use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
35
use lightning::chain::keysinterface::{ChannelKeys, KeysInterface};
46
use lightning::ln::channelmanager::ChannelManager;
5-
use lightning::{log_internal, log_trace};
67
use lightning::util::logger::Logger;
78
use lightning::util::ser::Writeable;
89
use std::sync::Arc;
@@ -28,14 +29,7 @@ pub struct BackgroundProcessor {
2829
stop_thread: Arc<AtomicBool>,
2930
/// May be used to retrieve and handle the error if `BackgroundProcessor`'s thread
3031
/// panics.
31-
pub thread_handle: JoinHandle<()>
32-
}
33-
34-
// Re-export these modules for the logging macros.
35-
mod util {
36-
pub(crate) mod logger {
37-
pub(crate) use lightning::util::logger::Record;
38-
}
32+
pub thread_handle: JoinHandle<Result<(), std::io::Error>>
3933
}
4034

4135
#[cfg(not(test))]
@@ -45,25 +39,20 @@ const CHAN_FRESHNESS_TIMER: u64 = 1;
4539

4640
impl BackgroundProcessor {
4741
/// Start a background thread that takes care of responsibilities enumerated in the top-level
48-
/// documentation. Marked as `must_use` because otherwise the result is dropped immediately,
49-
/// resulting in the thread being terminated.
50-
/// Important note: this thread will panic if invoking `persist_manager` results in an error (and
51-
/// `start()` will need to be called again to restart the `BackgroundProcessor`).
52-
/// There are 3 main options for handling this panic:
53-
/// * wait on [`thread_handle`]'s `join()`, handle the error
54-
/// * [configure] to abort on panic
55-
/// * write a custom `persist_manager` to handle the error so it never gets returned to
56-
/// `BackgroundProcessor`.
42+
/// documentation.
43+
///
44+
/// If `persist_manager` returns an error, then this thread will return said error (and `start()`
45+
/// will need to be called again to restart the `BackgroundProcessor`). Users should wait on
46+
/// [`thread_handle`]'s `join()` method to be able to tell if and when an error is returned, or
47+
/// implement `persist_manager` such that an error is never returned to the `BackgroundProcessor`
5748
///
5849
/// `persist_manager` is responsible for writing out the `ChannelManager` to disk, and/or uploading
5950
/// to one or more backup services. See [`ChannelManager::write`] for writing out a `ChannelManager`.
6051
/// See [`FilesystemPersister::persist_manager`] for Rust-Lightning's provided implementation.
6152
///
6253
/// [`thread_handle`]: struct.BackgroundProcessor.html#structfield.thread_handle
63-
/// [configure]: https://doc.rust-lang.org/edition-guide/rust-2018/error-handling-and-panics/aborting-on-panic.html
6454
/// [`ChannelManager::write`]: ../lightning/ln/channelmanager/struct.ChannelManager.html#method.write
6555
/// [`FilesystemPersister::persist_manager`]: ../lightning_persister/struct.FilesystemPersister.html#impl
66-
#[must_use]
6756
pub fn start<PM, ChanSigner, M, T, K, F, L>(persist_manager: PM, manager: Arc<ChannelManager<ChanSigner, Arc<M>, Arc<T>, Arc<K>, Arc<F>, Arc<L>>>, logger: Arc<L>) -> Self
6857
where ChanSigner: 'static + ChannelKeys + Writeable,
6958
M: 'static + chain::Watch<Keys=ChanSigner>,
@@ -75,19 +64,17 @@ impl BackgroundProcessor {
7564
{
7665
let stop_thread = Arc::new(AtomicBool::new(false));
7766
let stop_thread_clone = stop_thread.clone();
78-
let handle = thread::spawn(move || {
67+
let handle = thread::spawn(move || -> Result<(), std::io::Error>{
7968
let mut current_time = Instant::now();
8069
loop {
8170
let updates_available = manager.wait_timeout(Duration::from_millis(100));
8271
if updates_available {
83-
if let Err(e) = persist_manager(manager.clone()) {
84-
panic!("Errored persisting manager: {}", e);
85-
};
72+
persist_manager(manager.clone())?;
8673
}
8774
// Exit the loop if the background processor was requested to stop.
8875
if stop_thread.load(Ordering::Acquire) == true {
8976
log_trace!(logger, "Terminating background processor.");
90-
break;
77+
return Ok(())
9178
}
9279
if current_time.elapsed().as_secs() > CHAN_FRESHNESS_TIMER {
9380
log_trace!(logger, "Calling manager's timer_chan_freshness_every_min");
@@ -217,7 +204,7 @@ mod tests {
217204
// Initiate the background processors to watch each node.
218205
let data_dir = nodes[0].persister.get_data_dir();
219206
let callback = move |node| FilesystemPersister::persist_manager(data_dir.clone(), node);
220-
let _processor = BackgroundProcessor::start(callback, nodes[0].node.clone(), nodes[0].logger.clone());
207+
BackgroundProcessor::start(callback, nodes[0].node.clone(), nodes[0].logger.clone());
221208

222209
// Go through the channel creation process until each node should have something persisted.
223210
let tx = open_channel!(nodes[0], nodes[1], 100000);
@@ -270,7 +257,7 @@ mod tests {
270257
let nodes = create_nodes(1, "test_chan_freshness_called".to_string());
271258
let data_dir = nodes[0].persister.get_data_dir();
272259
let callback = move |node| FilesystemPersister::persist_manager(data_dir.clone(), node);
273-
let processor = BackgroundProcessor::start(callback, nodes[0].node.clone(), nodes[0].logger.clone());
260+
BackgroundProcessor::start(callback, nodes[0].node.clone(), nodes[0].logger.clone());
274261
loop {
275262
let log_entries = nodes[0].logger.lines.lock().unwrap();
276263
let desired_log = "Calling manager's timer_chan_freshness_every_min".to_string();
@@ -294,16 +281,6 @@ mod tests {
294281
Err(std::io::Error::new(std::io::ErrorKind::Other, "test"))
295282
}
296283

297-
// We don't want the expected panic to print to the console during testing, so
298-
// swallow it here.
299-
std::panic::set_hook(Box::new(|panic_info| {
300-
if let Some(s) = panic_info.payload().downcast_ref::<String>() {
301-
assert_eq!(s, "Errored persisting manager: test");
302-
} else {
303-
panic!("Expected string panic");
304-
}
305-
}));
306-
307284
let nodes = create_nodes(2, "test_persist_error".to_string());
308285
let bg_processor = BackgroundProcessor::start(persist_manager, nodes[0].node.clone(), nodes[0].logger.clone());
309286
open_channel!(nodes[0], nodes[1], 100000);

lightning/src/util/macro_logger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ macro_rules! log_spendable {
160160
#[macro_export]
161161
macro_rules! log_internal {
162162
($logger: expr, $lvl:expr, $($arg:tt)+) => (
163-
$logger.log(&::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
163+
$logger.log(&$crate::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
164164
);
165165
}
166166

0 commit comments

Comments
 (0)