Skip to content

Commit e195b37

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 e195b37

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/lib.rs

Lines changed: 11 additions & 10 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,14 +676,14 @@ 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.
678-
let timeout_res = tokio::task::block_in_place(move || {
679-
runtime.block_on(async {
680-
tokio::time::timeout(
681-
Duration::from_secs(100),
682-
event_handling_stopped_receiver.changed(),
683-
)
684-
.await
685-
})
679+
let stop_runtime =
680+
tokio::runtime::Builder::new_current_thread().enable_time().build().unwrap();
681+
let timeout_res = stop_runtime.block_on(async {
682+
tokio::time::timeout(
683+
Duration::from_secs(100),
684+
event_handling_stopped_receiver.changed(),
685+
)
686+
.await
686687
});
687688

688689
match timeout_res {

0 commit comments

Comments
 (0)