Skip to content

Commit 8aefd5b

Browse files
committed
Use current thread runtime for shutdown
Previously, we'd use the node's spawned multithread runtime to await the event handler finishing up. However, this would require a `block_on`/`block_in_place` dance, the former of which could still end up locking up if we happen to be out of blocking threads on shutdown. Here, we instead avoid touching the runtime and use a new current thread runtime to wait for event handler shutdown.
1 parent b914731 commit 8aefd5b

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,10 @@ impl Node {
643643
///
644644
/// After this returns most API methods will return [`Error::NotRunning`].
645645
pub fn stop(&self) -> Result<(), Error> {
646-
let runtime = self.runtime.write().unwrap().take().ok_or(Error::NotRunning)?;
646+
let mut runtime_lock = self.runtime.write().unwrap();
647+
let _runtime = runtime_lock.take().ok_or(Error::NotRunning)?;
647648
#[cfg(tokio_unstable)]
648-
let metrics_runtime = Arc::clone(&runtime);
649+
let metrics_runtime = Arc::clone(&_runtime);
649650

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

@@ -675,8 +676,10 @@ impl Node {
675676
// FIXME: For now, we wait up to 100 secs (BDK_WALLET_SYNC_TIMEOUT_SECS + 10) to allow
676677
// event handling to exit gracefully even if it was blocked on the BDK wallet syncing. We
677678
// should drop this considerably post upgrading to BDK 1.0.
679+
let stop_runtime =
680+
tokio::runtime::Builder::new_current_thread().enable_time().build().unwrap();
678681
let timeout_res = tokio::task::block_in_place(move || {
679-
runtime.block_on(async {
682+
stop_runtime.block_on(async {
680683
tokio::time::timeout(
681684
Duration::from_secs(100),
682685
event_handling_stopped_receiver.changed(),

0 commit comments

Comments
 (0)