@@ -10,13 +10,14 @@ use deno_tls::rustls::RootCertStore;
10
10
use deno_tls:: rustls_native_certs:: load_native_certs;
11
11
use deno_tls:: RootCertStoreProvider ;
12
12
use log:: error;
13
+ use sb_core:: conn_sync:: ConnSync ;
13
14
use serde:: de:: DeserializeOwned ;
14
15
use std:: collections:: HashMap ;
15
16
use std:: fmt;
17
+ use std:: os:: fd:: RawFd ;
16
18
use std:: sync:: Arc ;
17
- use std:: time:: Duration ;
18
19
use tokio:: net:: UnixStream ;
19
- use tokio:: sync:: mpsc;
20
+ use tokio:: sync:: { mpsc, watch } ;
20
21
21
22
use crate :: snapshot;
22
23
use event_worker:: events:: { EventMetadata , WorkerEventWithMetadata } ;
@@ -79,6 +80,7 @@ impl DenoRuntime {
79
80
maybe_eszip,
80
81
maybe_entrypoint,
81
82
maybe_module_code,
83
+ ..
82
84
} = opts;
83
85
84
86
let user_agent = "supabase-edge-runtime" . to_string ( ) ;
@@ -302,6 +304,10 @@ impl DenoRuntime {
302
304
. put :: < mpsc:: UnboundedReceiver < WorkerEventWithMetadata > > ( events_rx. unwrap ( ) ) ;
303
305
}
304
306
307
+ if conf. is_main_worker ( ) || conf. is_user_worker ( ) {
308
+ op_state. put :: < HashMap < RawFd , watch:: Receiver < ConnSync > > > ( HashMap :: new ( ) ) ;
309
+ }
310
+
305
311
if conf. is_user_worker ( ) {
306
312
let conf = conf. as_user_worker ( ) . unwrap ( ) ;
307
313
@@ -336,13 +342,16 @@ impl DenoRuntime {
336
342
}
337
343
338
344
pub async fn run (
339
- mut self ,
340
- unix_stream_rx : mpsc:: UnboundedReceiver < UnixStream > ,
345
+ & mut self ,
346
+ unix_stream_rx : mpsc:: UnboundedReceiver < ( UnixStream , Option < watch :: Receiver < ConnSync > > ) > ,
341
347
) -> Result < ( ) , Error > {
342
348
{
343
349
let op_state_rc = self . js_runtime . op_state ( ) ;
344
350
let mut op_state = op_state_rc. borrow_mut ( ) ;
345
- op_state. put :: < mpsc:: UnboundedReceiver < UnixStream > > ( unix_stream_rx) ;
351
+ op_state
352
+ . put :: < mpsc:: UnboundedReceiver < ( UnixStream , Option < watch:: Receiver < ConnSync > > ) > > (
353
+ unix_stream_rx,
354
+ ) ;
346
355
347
356
if self . conf . is_main_worker ( ) {
348
357
op_state. put :: < mpsc:: UnboundedSender < UserWorkerMsgs > > (
@@ -351,36 +360,19 @@ impl DenoRuntime {
351
360
}
352
361
}
353
362
354
- let mut js_runtime = self . js_runtime ;
363
+ let js_runtime = & mut self . js_runtime ;
364
+ let mod_result_rx = js_runtime. mod_evaluate ( self . main_module_id ) ;
355
365
356
- let future = async move {
357
- let mod_result_rx = js_runtime . mod_evaluate ( self . main_module_id ) ;
358
- match js_runtime . run_event_loop ( false ) . await {
359
- Err ( err ) => {
360
- // usually this happens because isolate is terminated
361
- error ! ( "event loop error: {}" , err) ;
362
- Err ( anyhow ! ( "event loop error: {}" , err) )
366
+ match js_runtime . run_event_loop ( false ) . await {
367
+ Err ( err ) => Err ( anyhow ! ( "event loop error: {}" , err ) ) ,
368
+ Ok ( _ ) => match mod_result_rx . await {
369
+ Err ( _ ) => Err ( anyhow ! ( "mod result sender dropped" ) ) ,
370
+ Ok ( Err ( err ) ) => {
371
+ error ! ( "{}" , err. to_string ( ) ) ;
372
+ Err ( err)
363
373
}
364
- Ok ( _) => match mod_result_rx. await {
365
- Err ( _) => Err ( anyhow ! ( "mod result sender dropped" ) ) ,
366
- Ok ( Err ( err) ) => {
367
- error ! ( "{}" , err. to_string( ) ) ;
368
- Err ( err)
369
- }
370
- Ok ( Ok ( _) ) => Ok ( ( ) ) ,
371
- } ,
372
- }
373
- } ;
374
-
375
- // need to set an explicit timeout here in case the event loop idle
376
- let mut duration = Duration :: MAX ;
377
- if self . conf . is_user_worker ( ) {
378
- let worker_timeout_ms = self . conf . as_user_worker ( ) . unwrap ( ) . worker_timeout_ms ;
379
- duration = Duration :: from_millis ( worker_timeout_ms) ;
380
- }
381
- match tokio:: time:: timeout ( duration, future) . await {
382
- Err ( _) => Err ( anyhow ! ( "wall clock duration reached" ) ) ,
383
- Ok ( res) => res,
374
+ Ok ( Ok ( _) ) => Ok ( ( ) ) ,
375
+ } ,
384
376
}
385
377
}
386
378
@@ -404,6 +396,7 @@ impl DenoRuntime {
404
396
mod test {
405
397
use crate :: deno_runtime:: DenoRuntime ;
406
398
use deno_core:: { FastString , ModuleCode } ;
399
+ use sb_core:: conn_sync:: ConnSync ;
407
400
use sb_graph:: emitter:: EmitterFactory ;
408
401
use sb_graph:: { generate_binary_eszip, EszipPayloadKind } ;
409
402
use sb_workers:: context:: {
@@ -417,7 +410,7 @@ mod test {
417
410
use std:: path:: PathBuf ;
418
411
use std:: sync:: Arc ;
419
412
use tokio:: net:: UnixStream ;
420
- use tokio:: sync:: mpsc;
413
+ use tokio:: sync:: { mpsc, watch } ;
421
414
422
415
#[ tokio:: test]
423
416
async fn test_module_code_no_eszip ( ) {
@@ -428,6 +421,7 @@ mod test {
428
421
import_map_path : None ,
429
422
env_vars : Default :: default ( ) ,
430
423
events_rx : None ,
424
+ timing : None ,
431
425
maybe_eszip : None ,
432
426
maybe_entrypoint : None ,
433
427
maybe_module_code : Some ( FastString :: from ( String :: from (
@@ -461,6 +455,7 @@ mod test {
461
455
import_map_path : None ,
462
456
env_vars : Default :: default ( ) ,
463
457
events_rx : None ,
458
+ timing : None ,
464
459
maybe_eszip : Some ( EszipPayloadKind :: VecKind ( eszip_code) ) ,
465
460
maybe_entrypoint : None ,
466
461
maybe_module_code : None ,
@@ -509,6 +504,7 @@ mod test {
509
504
import_map_path : None ,
510
505
env_vars : Default :: default ( ) ,
511
506
events_rx : None ,
507
+ timing : None ,
512
508
maybe_eszip : Some ( EszipPayloadKind :: VecKind ( eszip_code) ) ,
513
509
maybe_entrypoint : None ,
514
510
maybe_module_code : None ,
@@ -551,6 +547,7 @@ mod test {
551
547
import_map_path : None ,
552
548
env_vars : env_vars. unwrap_or_default ( ) ,
553
549
events_rx : None ,
550
+ timing : None ,
554
551
maybe_eszip : None ,
555
552
maybe_entrypoint : None ,
556
553
maybe_module_code : None ,
@@ -869,6 +866,7 @@ mod test {
869
866
key : None ,
870
867
pool_msg_tx : None ,
871
868
events_msg_tx : None ,
869
+ cancel : None ,
872
870
service_path : None ,
873
871
} ) ) ,
874
872
)
@@ -877,8 +875,10 @@ mod test {
877
875
878
876
#[ tokio:: test]
879
877
async fn test_read_file_user_rt ( ) {
880
- let user_rt = create_basic_user_runtime ( "./test_cases/readFile" , 20 , 1000 ) . await ;
881
- let ( _tx, unix_stream_rx) = mpsc:: unbounded_channel :: < UnixStream > ( ) ;
878
+ let mut user_rt = create_basic_user_runtime ( "./test_cases/readFile" , 20 , 1000 ) . await ;
879
+ let ( _tx, unix_stream_rx) =
880
+ mpsc:: unbounded_channel :: < ( UnixStream , Option < watch:: Receiver < ConnSync > > ) > ( ) ;
881
+
882
882
let result = user_rt. run ( unix_stream_rx) . await ;
883
883
match result {
884
884
Err ( err) => {
@@ -892,16 +892,18 @@ mod test {
892
892
893
893
#[ tokio:: test]
894
894
async fn test_array_buffer_allocation_below_limit ( ) {
895
- let user_rt = create_basic_user_runtime ( "./test_cases/array_buffers" , 20 , 1000 ) . await ;
896
- let ( _tx, unix_stream_rx) = mpsc:: unbounded_channel :: < UnixStream > ( ) ;
895
+ let mut user_rt = create_basic_user_runtime ( "./test_cases/array_buffers" , 20 , 1000 ) . await ;
896
+ let ( _tx, unix_stream_rx) =
897
+ mpsc:: unbounded_channel :: < ( UnixStream , Option < watch:: Receiver < ConnSync > > ) > ( ) ;
897
898
let result = user_rt. run ( unix_stream_rx) . await ;
898
899
assert ! ( result. is_ok( ) , "expected no errors" ) ;
899
900
}
900
901
901
902
#[ tokio:: test]
902
903
async fn test_array_buffer_allocation_above_limit ( ) {
903
- let user_rt = create_basic_user_runtime ( "./test_cases/array_buffers" , 15 , 1000 ) . await ;
904
- let ( _tx, unix_stream_rx) = mpsc:: unbounded_channel :: < UnixStream > ( ) ;
904
+ let mut user_rt = create_basic_user_runtime ( "./test_cases/array_buffers" , 15 , 1000 ) . await ;
905
+ let ( _tx, unix_stream_rx) =
906
+ mpsc:: unbounded_channel :: < ( UnixStream , Option < watch:: Receiver < ConnSync > > ) > ( ) ;
905
907
let result = user_rt. run ( unix_stream_rx) . await ;
906
908
match result {
907
909
Err ( err) => {
0 commit comments