Skip to content

Commit

Permalink
lib: non-blocking daemon shutdown
Browse files Browse the repository at this point in the history
This makes it possible to trigger the shutdown of the daemon through the
DaemonHandle, without having to block while waiting for the poller
thread to join.

Incidently, this allows to avoid having to move `self` which in turns
allows to fix a GUI bug (see
#622).
  • Loading branch information
darosior committed Aug 22, 2023
1 parent bdeaf93 commit 39d8980
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/bitcoin/poller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,22 @@ impl Poller {
Poller { shutdown, handle }
}

pub fn stop(self) {
pub fn trigger_stop(&self) {
self.shutdown.store(true, atomic::Ordering::Relaxed);
}

pub fn stop(self) {
self.trigger_stop();
self.handle.join().expect("The poller loop must not fail");
}

pub fn is_stopped(&self) -> bool {
// Doc says "This might return true for a brief moment after the thread’s main function has
// returned, but before the thread itself has stopped running.". But it's not an issue for
// us, as long as the main poller function has returned we are good.
self.handle.is_finished()
}

#[cfg(test)]
pub fn test_stop(&mut self) {
self.shutdown.store(true, atomic::Ordering::Relaxed);
Expand Down
12 changes: 11 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,22 @@ impl DaemonHandle {
Ok(())
}

// NOTE: this moves out the data as it should not be reused after shutdown
/// Shut down the Liana daemon.
pub fn shutdown(self) {
self.bitcoin_poller.stop();
}

/// Tell the daemon to shut down. This will return before the shutdown completes. The structure
/// must not be reused after triggering shutdown.
pub fn trigger_shutdown(&self) {
self.bitcoin_poller.trigger_stop()
}

/// Whether the daemon has finished shutting down.
pub fn shutdown_complete(&self) -> bool {
self.bitcoin_poller.is_stopped()
}

// We need a shutdown utility that does not move for implementing Drop for the DummyLiana
#[cfg(test)]
pub fn test_shutdown(&mut self) {
Expand Down

0 comments on commit 39d8980

Please sign in to comment.