11use crate :: inspector_server:: Inspector ;
2- use crate :: rt_worker:: rt;
32use crate :: rt_worker:: supervisor:: { CPUUsage , CPUUsageMetrics } ;
43use crate :: rt_worker:: worker:: DuplexStreamEntry ;
54use crate :: utils:: units:: { bytes_to_display, mib_to_bytes} ;
@@ -25,6 +24,7 @@ use futures_util::future::poll_fn;
2524use futures_util:: task:: AtomicWaker ;
2625use log:: { error, trace} ;
2726use once_cell:: sync:: { Lazy , OnceCell } ;
27+ use sb_core:: conn_sync:: DenoRuntimeDropToken ;
2828use sb_core:: http:: sb_core_http;
2929use sb_core:: http_start:: sb_core_http_start;
3030use sb_core:: util:: sync:: AtomicFlag ;
@@ -184,6 +184,7 @@ impl GetRuntimeContext for () {
184184}
185185
186186pub struct DenoRuntime < RuntimeContext = ( ) > {
187+ pub drop_token : CancellationToken ,
187188 pub js_runtime : JsRuntime ,
188189 pub env_vars : HashMap < String , String > , // TODO: does this need to be pub?
189190 pub conf : WorkerRuntimeOpts ,
@@ -203,6 +204,8 @@ pub struct DenoRuntime<RuntimeContext = ()> {
203204
204205impl < RuntimeContext > Drop for DenoRuntime < RuntimeContext > {
205206 fn drop ( & mut self ) {
207+ self . drop_token . cancel ( ) ;
208+
206209 if self . conf . is_user_worker ( ) {
207210 self . js_runtime . v8_isolate ( ) . remove_gc_prologue_callback (
208211 mem_check_gc_prologue_callback_fn,
@@ -238,13 +241,16 @@ where
238241 ..
239242 } = opts;
240243
244+ let drop_token = CancellationToken :: default ( ) ;
245+
241246 let base_dir_path = std:: env:: current_dir ( ) . map ( |p| p. join ( & service_path) ) ?;
242247 let base_url = Url :: from_directory_path ( & base_dir_path) . unwrap ( ) ;
243248
244249 let is_user_worker = conf. is_user_worker ( ) ;
245250
246251 let potential_exts = vec ! [ "ts" , "tsx" , "js" , "jsx" ] ;
247252 let mut main_module_url = base_url. join ( "index.ts" ) ?;
253+
248254 for potential_ext in potential_exts {
249255 main_module_url = base_url. join ( format ! ( "index.{}" , potential_ext) . as_str ( ) ) ?;
250256 if main_module_url. to_file_path ( ) . unwrap ( ) . exists ( ) {
@@ -587,6 +593,7 @@ where
587593 }
588594
589595 op_state. put :: < sb_env:: EnvVars > ( env_vars) ;
596+ op_state. put ( DenoRuntimeDropToken ( drop_token. clone ( ) ) )
590597 }
591598
592599 let main_module_id = {
@@ -600,7 +607,7 @@ where
600607 } ;
601608
602609 if is_user_worker {
603- drop ( rt :: SUPERVISOR_RT . spawn ( {
610+ drop ( base_rt :: SUPERVISOR_RT . spawn ( {
604611 let drop_token = mem_check. drop_token . clone ( ) ;
605612 let waker = mem_check. waker . clone ( ) ;
606613
@@ -624,6 +631,7 @@ where
624631 }
625632
626633 Ok ( Self {
634+ drop_token,
627635 js_runtime,
628636 env_vars,
629637 conf,
@@ -866,7 +874,7 @@ where
866874 let drop_token = self . mem_check . drop_token . clone ( ) ;
867875 let state = self . mem_check_state ( ) ;
868876
869- drop ( rt :: SUPERVISOR_RT . spawn ( async move {
877+ drop ( base_rt :: SUPERVISOR_RT . spawn ( async move {
870878 loop {
871879 tokio:: select! {
872880 _ = notify. notified( ) => {
0 commit comments