@@ -29,7 +29,16 @@ impl Runtime {
2929 let mode = match tokio:: runtime:: Handle :: try_current ( ) {
3030 Ok ( handle) => RuntimeMode :: Handle ( handle) ,
3131 Err ( _) => {
32- let rt = tokio:: runtime:: Builder :: new_multi_thread ( ) . enable_all ( ) . build ( ) ?;
32+ let rt = tokio:: runtime:: Builder :: new_multi_thread ( )
33+ . thread_name_fn ( || {
34+ "MY-CUSTOM" . to_owned ( )
35+ } )
36+ . worker_threads ( 5 )
37+ . max_blocking_threads ( 20 )
38+ . on_thread_start ( || {
39+ println ! ( "THREAD started" ) ;
40+ } )
41+ . enable_all ( ) . build ( ) ?;
3342 RuntimeMode :: Owned ( rt)
3443 } ,
3544 } ;
@@ -67,7 +76,7 @@ impl Runtime {
6776 {
6877 let mut background_tasks = self . background_tasks . lock ( ) . unwrap ( ) ;
6978 let runtime_handle = self . handle ( ) ;
70- background_tasks. spawn_on ( future, runtime_handle) ;
79+ background_tasks. spawn_on ( async { future. await } , runtime_handle) ;
7180 }
7281
7382 pub fn spawn_cancellable_background_task < F > ( & self , future : F )
@@ -76,7 +85,7 @@ impl Runtime {
7685 {
7786 let mut cancellable_background_tasks = self . cancellable_background_tasks . lock ( ) . unwrap ( ) ;
7887 let runtime_handle = self . handle ( ) ;
79- cancellable_background_tasks. spawn_on ( future, runtime_handle) ;
88+ cancellable_background_tasks. spawn_on ( async { future. await } , runtime_handle) ;
8089 }
8190
8291 pub fn spawn_background_processor_task < F > ( & self , future : F )
@@ -87,7 +96,7 @@ impl Runtime {
8796 debug_assert ! ( background_processor_task. is_none( ) , "Expected no background processor_task" ) ;
8897
8998 let runtime_handle = self . handle ( ) ;
90- let handle = runtime_handle. spawn ( future) ;
99+ let handle = runtime_handle. spawn ( async { future. await } ) ;
91100 * background_processor_task = Some ( handle) ;
92101 }
93102
@@ -106,21 +115,39 @@ impl Runtime {
106115 // during `block_on`, as this is the context `block_in_place` would operate on. So we try
107116 // to detect the outer context here, and otherwise use whatever was set during
108117 // initialization.
109- let handle = tokio:: runtime:: Handle :: try_current ( ) . unwrap_or ( self . handle ( ) . clone ( ) ) ;
110- tokio:: task:: block_in_place ( move || handle. block_on ( future) )
118+ let runtime_handle = tokio:: runtime:: Handle :: try_current ( ) . unwrap_or ( self . handle ( ) . clone ( ) ) ;
119+ tokio:: task:: block_in_place ( move || runtime_handle. block_on ( async { future. await } ) )
120+ }
121+
122+ pub fn spawn_block_on < F : Future + Send + ' static > ( & self , future : F , write_id : u64 ) -> F :: Output
123+ where
124+ <F as std:: future:: Future >:: Output : Send + std:: fmt:: Debug ,
125+ {
126+ // While we generally decided not to overthink via which call graph users would enter our
127+ // runtime context, we'd still try to reuse whatever current context would be present
128+ // during `block_on`, as this is the context `block_in_place` would operate on. So we try
129+ // to detect the outer context here, and otherwise use whatever was set during
130+ // initialization.
131+ let runtime_handle = tokio:: runtime:: Handle :: try_current ( ) . unwrap_or ( self . handle ( ) . clone ( ) ) ;
132+ println ! ( "RUNTIME STATS BEFORE: {} workers, {} blocking queue depth" , runtime_handle. metrics( ) . num_workers( ) , runtime_handle. metrics( ) . blocking_queue_depth( ) ) ;
133+ let res = tokio:: task:: block_in_place ( move || runtime_handle. block_on ( async { future. await } ) ) ;
134+ let runtime_handle_2 = tokio:: runtime:: Handle :: try_current ( ) . unwrap_or ( self . handle ( ) . clone ( ) ) ;
135+ println ! ( "WRITE {} AFTER block_in_place" , write_id) ;
136+ println ! ( "RUNTIME STATS AFTER: {} workers, {} blocking queue depth" , runtime_handle_2. metrics( ) . num_workers( ) , runtime_handle_2. metrics( ) . blocking_queue_depth( ) ) ;
137+ res
111138 }
112139
113140 pub fn abort_cancellable_background_tasks ( & self ) {
114141 let mut tasks = core:: mem:: take ( & mut * self . cancellable_background_tasks . lock ( ) . unwrap ( ) ) ;
115142 debug_assert ! ( tasks. len( ) > 0 , "Expected some cancellable background_tasks" ) ;
116143 tasks. abort_all ( ) ;
117- self . block_on ( async { while let Some ( _) = tasks. join_next ( ) . await { } } )
144+ self . block_on ( async move { while let Some ( _) = tasks. join_next ( ) . await { } } )
118145 }
119146
120147 pub fn wait_on_background_tasks ( & self ) {
121148 let mut tasks = core:: mem:: take ( & mut * self . background_tasks . lock ( ) . unwrap ( ) ) ;
122149 debug_assert ! ( tasks. len( ) > 0 , "Expected some background_tasks" ) ;
123- self . block_on ( async {
150+ self . block_on ( async move {
124151 loop {
125152 let timeout_fut = tokio:: time:: timeout (
126153 Duration :: from_secs ( BACKGROUND_TASK_SHUTDOWN_TIMEOUT_SECS ) ,
@@ -154,7 +181,7 @@ impl Runtime {
154181 self . background_processor_task . lock ( ) . unwrap ( ) . take ( )
155182 {
156183 let abort_handle = background_processor_task. abort_handle ( ) ;
157- let timeout_res = self . block_on ( async {
184+ let timeout_res = self . block_on ( async move {
158185 tokio:: time:: timeout (
159186 Duration :: from_secs ( LDK_EVENT_HANDLER_SHUTDOWN_TIMEOUT_SECS ) ,
160187 background_processor_task,
0 commit comments