Skip to content

Commit ed094cf

Browse files
goffrieConvex, Inc.
authored andcommitted
Check for isolate memory carry-over after creating the fresh V8 context (#37585)
GitOrigin-RevId: 454bc3ebc659c3a4e35e6e758187b85dcc3637b6
1 parent 1edfd41 commit ed094cf

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

crates/isolate/src/client.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,7 @@ pub trait IsolateWorker<RT: Runtime>: Clone + Send + 'static {
13821382
let mut ready: Option<(oneshot::Sender<_>, _)> = None;
13831383
'recreate_isolate: loop {
13841384
let mut last_client_id: Option<String> = None;
1385+
let mut last_request: Option<String> = None;
13851386
let mut isolate = Isolate::new(self.rt(), *max_user_timeout, limiter.clone());
13861387
heap_stats.store(isolate.heap_stats());
13871388
loop {
@@ -1391,6 +1392,14 @@ pub trait IsolateWorker<RT: Runtime>: Clone + Send + 'static {
13911392
let context = v8::Context::new(&mut scope, v8::ContextOptions::default());
13921393
v8::Global::new(&mut scope, context)
13931394
};
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());
13941403
if let Some((done, done_token)) = ready.take() {
13951404
// Inform the scheduler that this thread is ready to accept a new request.
13961405
let _ = done.send(done_token);
@@ -1445,10 +1454,10 @@ pub trait IsolateWorker<RT: Runtime>: Clone + Send + 'static {
14451454
)
14461455
.in_span(root)
14471456
.await;
1448-
if !isolate_clean || should_recreate_isolate(&mut isolate, debug_str) {
1457+
if !isolate_clean || should_recreate_isolate(&mut isolate, &debug_str) {
14491458
continue 'recreate_isolate;
14501459
}
1451-
heap_stats.store(isolate.heap_stats());
1460+
last_request = Some(debug_str);
14521461
}
14531462
}
14541463
}
@@ -1470,7 +1479,7 @@ pub trait IsolateWorker<RT: Runtime>: Clone + Send + 'static {
14701479

14711480
pub(crate) fn should_recreate_isolate<RT: Runtime>(
14721481
isolate: &mut Isolate<RT>,
1473-
last_executed: String,
1482+
last_executed: &str,
14741483
) -> bool {
14751484
if !*REUSE_ISOLATES {
14761485
metrics::log_recreate_isolate("env_disabled");

0 commit comments

Comments
 (0)