@@ -1382,6 +1382,7 @@ pub trait IsolateWorker<RT: Runtime>: Clone + Send + 'static {
1382
1382
let mut ready: Option < ( oneshot:: Sender < _ > , _ ) > = None ;
1383
1383
' recreate_isolate: loop {
1384
1384
let mut last_client_id: Option < String > = None ;
1385
+ let mut last_request: Option < String > = None ;
1385
1386
let mut isolate = Isolate :: new ( self . rt ( ) , * max_user_timeout, limiter. clone ( ) ) ;
1386
1387
heap_stats. store ( isolate. heap_stats ( ) ) ;
1387
1388
loop {
@@ -1391,6 +1392,14 @@ pub trait IsolateWorker<RT: Runtime>: Clone + Send + 'static {
1391
1392
let context = v8:: Context :: new ( & mut scope, v8:: ContextOptions :: default ( ) ) ;
1392
1393
v8:: Global :: new ( & mut scope, context)
1393
1394
} ;
1395
+ // Check again whether the isolate has enough free heap memory
1396
+ // before starting the next request
1397
+ if let Some ( debug_str) = & last_request
1398
+ && should_recreate_isolate ( & mut isolate, debug_str)
1399
+ {
1400
+ continue ' recreate_isolate;
1401
+ }
1402
+ heap_stats. store ( isolate. heap_stats ( ) ) ;
1394
1403
if let Some ( ( done, done_token) ) = ready. take ( ) {
1395
1404
// Inform the scheduler that this thread is ready to accept a new request.
1396
1405
let _ = done. send ( done_token) ;
@@ -1445,10 +1454,10 @@ pub trait IsolateWorker<RT: Runtime>: Clone + Send + 'static {
1445
1454
)
1446
1455
. in_span( root)
1447
1456
. await ;
1448
- if !isolate_clean || should_recreate_isolate( & mut isolate, debug_str) {
1457
+ if !isolate_clean || should_recreate_isolate( & mut isolate, & debug_str) {
1449
1458
continue ' recreate_isolate;
1450
1459
}
1451
- heap_stats . store ( isolate . heap_stats ( ) ) ;
1460
+ last_request = Some ( debug_str ) ;
1452
1461
}
1453
1462
}
1454
1463
}
@@ -1470,7 +1479,7 @@ pub trait IsolateWorker<RT: Runtime>: Clone + Send + 'static {
1470
1479
1471
1480
pub ( crate ) fn should_recreate_isolate < RT : Runtime > (
1472
1481
isolate : & mut Isolate < RT > ,
1473
- last_executed : String ,
1482
+ last_executed : & str ,
1474
1483
) -> bool {
1475
1484
if !* REUSE_ISOLATES {
1476
1485
metrics:: log_recreate_isolate ( "env_disabled" ) ;
0 commit comments