Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1668,11 +1668,13 @@ fn build_with_store_internal(
};

let (stop_sender, _) = tokio::sync::watch::channel(());
let (background_processor_stop_sender, _) = tokio::sync::watch::channel(());
let is_running = Arc::new(RwLock::new(false));

Ok(Node {
runtime,
stop_sender,
background_processor_stop_sender,
config,
wallet,
chain_source,
Expand Down
39 changes: 27 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ uniffi::include_scaffolding!("ldk_node");
pub struct Node {
runtime: Arc<Runtime>,
stop_sender: tokio::sync::watch::Sender<()>,
background_processor_stop_sender: tokio::sync::watch::Sender<()>,
config: Arc<Config>,
wallet: Arc<Wallet>,
chain_source: Arc<ChainSource>,
Expand Down Expand Up @@ -525,7 +526,7 @@ impl Node {
let background_logger = Arc::clone(&self.logger);
let background_error_logger = Arc::clone(&self.logger);
let background_scorer = Arc::clone(&self.scorer);
let stop_bp = self.stop_sender.subscribe();
let stop_bp = self.background_processor_stop_sender.subscribe();
let sleeper_logger = Arc::clone(&self.logger);
let sleeper = move |d| {
let mut stop = stop_bp.clone();
Expand Down Expand Up @@ -604,21 +605,20 @@ impl Node {

log_info!(self.logger, "Shutting down LDK Node with node ID {}...", self.node_id());

// Stop any runtime-dependant chain sources.
self.chain_source.stop();

// Stop the runtime.
match self.stop_sender.send(()) {
Ok(_) => log_trace!(self.logger, "Sent shutdown signal to background tasks."),
Err(e) => {
// Stop background tasks.
self.stop_sender
.send(())
.map(|_| {
log_trace!(self.logger, "Sent shutdown signal to background tasks.");
})
.unwrap_or_else(|e| {
log_error!(
self.logger,
"Failed to send shutdown signal. This should never happen: {}",
e
);
debug_assert!(false);
},
}
});

// Cancel cancellable background tasks
self.runtime.abort_cancellable_background_tasks();
Expand All @@ -627,12 +627,27 @@ impl Node {
self.peer_manager.disconnect_all_peers();
log_debug!(self.logger, "Disconnected all network peers.");

// Wait until non-cancellable background tasks (mod LDK's background processor) are done.
self.runtime.wait_on_background_tasks();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This seems like we'll end up breaking it when (eventually) electrum is async and we keep the stop signal for whatever reason but its running on the background task join set. I mean we'll notice quick cause all the stops will time out and fail in tests, but just seems weird to do this before initializing stopping another thing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, but chain_source.stop() doesn't actually stop/abort any of the running blocking tasks. So it's preferable to wait for the chain syncing to stop (or worst case timeout) before calling chain_source.stop(), which drops access to the electrum client. But, we're gonna do more refactorings soon that will hopefully allow us to get rid of the chain_source.stop() concept entirely.


// Stop any runtime-dependant chain sources.
self.chain_source.stop();
log_debug!(self.logger, "Stopped chain sources.");

// Wait until non-cancellable background tasks (mod LDK's background processor) are done.
self.runtime.wait_on_background_tasks();
// Stop the background processor.
self.background_processor_stop_sender
.send(())
.map(|_| {
log_trace!(self.logger, "Sent shutdown signal to background processor.");
})
.unwrap_or_else(|e| {
log_error!(
self.logger,
"Failed to send shutdown signal. This should never happen: {}",
e
);
debug_assert!(false);
});

// Finally, wait until background processing stopped, at least until a timeout is reached.
self.runtime.wait_on_background_processor_task();
Expand Down
Loading