@@ -171,7 +171,7 @@ uniffi::include_scaffolding!("ldk_node");
171171///
172172/// Needs to be initialized and instantiated through [`Builder::build`].
173173pub struct Node {
174- runtime : Arc < RwLock < Option < tokio:: runtime:: Runtime > > > ,
174+ runtime : Arc < RwLock < Option < Arc < tokio:: runtime:: Runtime > > > > ,
175175 stop_sender : tokio:: sync:: watch:: Sender < ( ) > ,
176176 event_handling_stopped_sender : tokio:: sync:: watch:: Sender < ( ) > ,
177177 config : Arc < Config > ,
@@ -211,6 +211,20 @@ impl Node {
211211 /// After this returns, the [`Node`] instance can be controlled via the provided API methods in
212212 /// a thread-safe manner.
213213 pub fn start ( & self ) -> Result < ( ) , Error > {
214+ let runtime =
215+ Arc :: new ( tokio:: runtime:: Builder :: new_multi_thread ( ) . enable_all ( ) . build ( ) . unwrap ( ) ) ;
216+ self . start_with_runtime ( runtime)
217+ }
218+
219+ /// Starts the necessary background tasks (such as handling events coming from user input,
220+ /// LDK/BDK, and the peer-to-peer network) on the the given `runtime`.
221+ ///
222+ /// This allows to have LDK Node reuse an outer pre-existing runtime, e.g., to avoid stacking Tokio
223+ /// runtime contexts.
224+ ///
225+ /// After this returns, the [`Node`] instance can be controlled via the provided API methods in
226+ /// a thread-safe manner.
227+ pub fn start_with_runtime ( & self , runtime : Arc < tokio:: runtime:: Runtime > ) -> Result < ( ) , Error > {
214228 // Acquire a run lock and hold it until we're setup.
215229 let mut runtime_lock = self . runtime . write ( ) . unwrap ( ) ;
216230 if runtime_lock. is_some ( ) {
@@ -225,8 +239,6 @@ impl Node {
225239 self . config. network
226240 ) ;
227241
228- let runtime = tokio:: runtime:: Builder :: new_multi_thread ( ) . enable_all ( ) . build ( ) . unwrap ( ) ;
229-
230242 // Block to ensure we update our fee rate cache once on startup
231243 let fee_estimator = Arc :: clone ( & self . fee_estimator ) ;
232244 let sync_logger = Arc :: clone ( & self . logger ) ;
@@ -862,9 +874,6 @@ impl Node {
862874 ) ;
863875 }
864876
865- // Shutdown our runtime. By now ~no or only very few tasks should be left.
866- runtime. shutdown_timeout ( Duration :: from_secs ( 10 ) ) ;
867-
868877 log_info ! ( self . logger, "Shutdown complete." ) ;
869878 Ok ( ( ) )
870879 }
0 commit comments