@@ -25,6 +25,7 @@ use base_rt::DenoRuntimeDropToken;
25
25
use base_rt:: DropToken ;
26
26
use base_rt:: RuntimeOtelExtraAttributes ;
27
27
use base_rt:: RuntimeState ;
28
+ use base_rt:: RuntimeWaker ;
28
29
use cooked_waker:: IntoWaker ;
29
30
use cooked_waker:: WakeRef ;
30
31
use cpu_timer:: CPUTimer ;
@@ -87,6 +88,7 @@ use ext_runtime::MemCheckWaker;
87
88
use ext_runtime:: PromiseMetrics ;
88
89
use ext_workers:: context:: UserWorkerMsgs ;
89
90
use ext_workers:: context:: WorkerContextInitOpts ;
91
+ use ext_workers:: context:: WorkerKind ;
90
92
use ext_workers:: context:: WorkerRuntimeOpts ;
91
93
use fs:: deno_compile_fs:: DenoCompileFileSystem ;
92
94
use fs:: prefix_fs:: PrefixFs ;
@@ -159,6 +161,11 @@ pub static SHOULD_INCLUDE_MALLOCED_MEMORY_ON_MEMCHECK: OnceCell<bool> =
159
161
OnceCell :: new ( ) ;
160
162
pub static MAYBE_DENO_VERSION : OnceCell < String > = OnceCell :: new ( ) ;
161
163
164
+ pub static MAIN_WORKER_INITIAL_HEAP_SIZE_MIB : OnceCell < u64 > = OnceCell :: new ( ) ;
165
+ pub static MAIN_WORKER_MAX_HEAP_SIZE_MIB : OnceCell < u64 > = OnceCell :: new ( ) ;
166
+ pub static EVENT_WORKER_INITIAL_HEAP_SIZE_MIB : OnceCell < u64 > = OnceCell :: new ( ) ;
167
+ pub static EVENT_WORKER_MAX_HEAP_SIZE_MIB : OnceCell < u64 > = OnceCell :: new ( ) ;
168
+
162
169
#[ ctor]
163
170
fn init_v8_platform ( ) {
164
171
set_v8_flags ( ) ;
@@ -387,6 +394,21 @@ fn cleanup_js_runtime(runtime: &mut JsRuntime) {
387
394
}
388
395
}
389
396
397
+ fn cleanup_js_runtime ( runtime : & mut JsRuntime ) {
398
+ let isolate = runtime. v8_isolate ( ) ;
399
+
400
+ assert_isolate_not_locked ( isolate) ;
401
+ let locker = unsafe {
402
+ Locker :: new ( std:: mem:: transmute :: < & mut Isolate , & mut Isolate > ( isolate) )
403
+ } ;
404
+
405
+ isolate. set_slot ( locker) ;
406
+
407
+ {
408
+ let _scope = runtime. handle_scope ( ) ;
409
+ }
410
+ }
411
+
390
412
pub struct DenoRuntime < RuntimeContext = DefaultRuntimeContext > {
391
413
pub runtime_state : Arc < RuntimeState > ,
392
414
pub js_runtime : ManuallyDrop < JsRuntime > ,
@@ -471,6 +493,7 @@ where
471
493
..
472
494
} = init_opts. unwrap ( ) ;
473
495
496
+ let waker = Arc :: < AtomicWaker > :: default ( ) ;
474
497
let drop_token = CancellationToken :: default ( ) ;
475
498
let is_user_worker = conf. is_user_worker ( ) ;
476
499
let is_some_entry_point = maybe_entrypoint. is_some ( ) ;
@@ -486,6 +509,7 @@ where
486
509
. unwrap_or_else ( || get_default_permissions ( conf. to_worker_kind ( ) ) ) ;
487
510
488
511
struct Bootstrap {
512
+ waker : Arc < AtomicWaker > ,
489
513
js_runtime : JsRuntime ,
490
514
mem_check : Arc < MemCheck > ,
491
515
has_inspector : bool ,
@@ -662,15 +686,22 @@ where
662
686
let tmp_fs =
663
687
TmpFs :: try_from ( maybe_tmp_fs_config. unwrap_or_default ( ) ) ?;
664
688
let tmp_fs_actual_path = tmp_fs. actual_path ( ) . to_path_buf ( ) ;
665
- let fs = PrefixFs :: new ( "/tmp" , tmp_fs. clone ( ) , Some ( base_fs) )
689
+ let mut fs = PrefixFs :: new ( "/tmp" , tmp_fs. clone ( ) , Some ( base_fs) )
666
690
. tmp_dir ( "/tmp" )
667
691
. add_fs ( tmp_fs_actual_path, tmp_fs) ;
668
692
693
+ fs
694
+ . set_runtime_state ( & runtime_state) ;
695
+
669
696
Ok (
670
697
if let Some ( s3_fs) =
671
698
maybe_s3_fs_config. map ( S3Fs :: new) . transpose ( ) ?
672
699
{
673
- ( Arc :: new ( fs. add_fs ( "/s3" , s3_fs. clone ( ) ) ) , Some ( s3_fs) )
700
+ let mut s3_prefix_fs = fs. add_fs ( "/s3" , s3_fs. clone ( ) ) ;
701
+
702
+ s3_prefix_fs. set_check_sync_api ( is_user_worker) ;
703
+
704
+ ( Arc :: new ( s3_prefix_fs) , Some ( s3_fs) )
674
705
} else {
675
706
( Arc :: new ( fs) , None )
676
707
} ,
@@ -779,39 +810,65 @@ where
779
810
let beforeunload_mem_threshold =
780
811
ArcSwapOption :: < u64 > :: from_pointee ( None ) ;
781
812
782
- if conf. is_user_worker ( ) {
783
- let conf = maybe_user_conf. unwrap ( ) ;
784
- let memory_limit_bytes = mib_to_bytes ( conf. memory_limit_mb ) as usize ;
813
+ match conf. to_worker_kind ( ) {
814
+ WorkerKind :: UserWorker => {
815
+ let conf = maybe_user_conf. unwrap ( ) ;
816
+ let memory_limit_bytes = mib_to_bytes ( conf. memory_limit_mb ) as usize ;
785
817
786
- beforeunload_mem_threshold. store (
787
- flags
788
- . beforeunload_memory_pct
789
- . and_then ( |it| percentage_value ( memory_limit_bytes as u64 , it) )
790
- . map ( Arc :: new) ,
791
- ) ;
792
-
793
- if conf. cpu_time_hard_limit_ms > 0 {
794
- beforeunload_cpu_threshold. store (
818
+ beforeunload_mem_threshold. store (
795
819
flags
796
- . beforeunload_cpu_pct
797
- . and_then ( |it| {
798
- percentage_value ( conf. cpu_time_hard_limit_ms , it)
799
- } )
820
+ . beforeunload_memory_pct
821
+ . and_then ( |it| percentage_value ( memory_limit_bytes as u64 , it) )
800
822
. map ( Arc :: new) ,
801
823
) ;
824
+
825
+ if conf. cpu_time_hard_limit_ms > 0 {
826
+ beforeunload_cpu_threshold. store (
827
+ flags
828
+ . beforeunload_cpu_pct
829
+ . and_then ( |it| {
830
+ percentage_value ( conf. cpu_time_hard_limit_ms , it)
831
+ } )
832
+ . map ( Arc :: new) ,
833
+ ) ;
834
+ }
835
+
836
+ let allocator = CustomAllocator :: new ( memory_limit_bytes) ;
837
+
838
+ allocator. set_waker ( mem_check. waker . clone ( ) ) ;
839
+
840
+ mem_check. limit = Some ( memory_limit_bytes) ;
841
+ create_params = Some (
842
+ v8:: CreateParams :: default ( )
843
+ . heap_limits ( mib_to_bytes ( 0 ) as usize , memory_limit_bytes)
844
+ . array_buffer_allocator ( allocator. into_v8_allocator ( ) ) ,
845
+ )
802
846
}
803
847
804
- let allocator = CustomAllocator :: new ( memory_limit_bytes) ;
848
+ kind => {
849
+ assert_ne ! ( kind, WorkerKind :: UserWorker ) ;
850
+ let initial_heap_size = match kind {
851
+ WorkerKind :: MainWorker => & MAIN_WORKER_INITIAL_HEAP_SIZE_MIB ,
852
+ WorkerKind :: EventsWorker => & EVENT_WORKER_INITIAL_HEAP_SIZE_MIB ,
853
+ _ => unreachable ! ( ) ,
854
+ } ;
855
+ let max_heap_size = match kind {
856
+ WorkerKind :: MainWorker => & MAIN_WORKER_MAX_HEAP_SIZE_MIB ,
857
+ WorkerKind :: EventsWorker => & EVENT_WORKER_MAX_HEAP_SIZE_MIB ,
858
+ _ => unreachable ! ( ) ,
859
+ } ;
805
860
806
- allocator. set_waker ( mem_check. waker . clone ( ) ) ;
861
+ let initial_heap_size = initial_heap_size. get ( ) . cloned ( ) . unwrap_or_default ( ) ;
862
+ let max_heap_size = max_heap_size. get ( ) . cloned ( ) . unwrap_or_default ( ) ;
807
863
808
- mem_check. limit = Some ( memory_limit_bytes) ;
809
- create_params = Some (
810
- v8:: CreateParams :: default ( )
811
- . heap_limits ( mib_to_bytes ( 0 ) as usize , memory_limit_bytes)
812
- . array_buffer_allocator ( allocator. into_v8_allocator ( ) ) ,
813
- )
814
- } ;
864
+ if max_heap_size > 0 {
865
+ create_params = Some ( v8:: CreateParams :: default ( ) . heap_limits (
866
+ mib_to_bytes ( initial_heap_size) as usize ,
867
+ mib_to_bytes ( max_heap_size) as usize ,
868
+ ) ) ;
869
+ }
870
+ }
871
+ }
815
872
816
873
let mem_check = Arc :: new ( mem_check) ;
817
874
let runtime_options = RuntimeOptions {
@@ -879,6 +936,7 @@ where
879
936
op_state. put ( promise_metrics. clone ( ) ) ;
880
937
op_state. put ( runtime_state. clone ( ) ) ;
881
938
op_state. put ( GlobalMainContext ( main_context) ) ;
939
+ op_state. put ( RuntimeWaker ( waker. clone ( ) ) )
882
940
}
883
941
884
942
{
@@ -913,6 +971,7 @@ where
913
971
}
914
972
915
973
Ok ( Bootstrap {
974
+ waker,
916
975
js_runtime,
917
976
mem_check,
918
977
has_inspector,
@@ -1029,6 +1088,7 @@ where
1029
1088
. await ;
1030
1089
1031
1090
let Bootstrap {
1091
+ waker,
1032
1092
mut js_runtime,
1033
1093
mem_check,
1034
1094
main_module_url,
@@ -1169,7 +1229,7 @@ where
1169
1229
promise_metrics,
1170
1230
1171
1231
mem_check,
1172
- waker : Arc :: default ( ) ,
1232
+ waker,
1173
1233
1174
1234
beforeunload_cpu_threshold : Arc :: new ( beforeunload_cpu_threshold) ,
1175
1235
beforeunload_mem_threshold : Arc :: new ( beforeunload_mem_threshold) ,
0 commit comments