Skip to content

Commit a173473

Browse files
committed
src: flush V8 interrupts from Environment dtor
This avoids an edge-case memory leak. Refs: #32523 (comment) PR-URL: #32523 Reviewed-By: Matheus Marchini <mat@mmarchini.me> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 6f9f546 commit a173473

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/env.cc

+25-1
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ using v8::NewStringType;
4343
using v8::Number;
4444
using v8::Object;
4545
using v8::Private;
46+
using v8::Script;
4647
using v8::SnapshotCreator;
4748
using v8::StackTrace;
4849
using v8::String;
4950
using v8::Symbol;
5051
using v8::TracingController;
52+
using v8::TryCatch;
5153
using v8::Undefined;
5254
using v8::Value;
5355
using worker::Worker;
@@ -435,9 +437,31 @@ Environment::Environment(IsolateData* isolate_data,
435437
}
436438

437439
Environment::~Environment() {
438-
if (Environment** interrupt_data = interrupt_data_.load())
440+
if (Environment** interrupt_data = interrupt_data_.load()) {
441+
// There are pending RequestInterrupt() callbacks. Tell them not to run,
442+
// then force V8 to run interrupts by compiling and running an empty script
443+
// so as not to leak memory.
439444
*interrupt_data = nullptr;
440445

446+
Isolate::AllowJavascriptExecutionScope allow_js_here(isolate());
447+
HandleScope handle_scope(isolate());
448+
TryCatch try_catch(isolate());
449+
Context::Scope context_scope(context());
450+
451+
#ifdef DEBUG
452+
bool consistency_check = false;
453+
isolate()->RequestInterrupt([](Isolate*, void* data) {
454+
*static_cast<bool*>(data) = true;
455+
}, &consistency_check);
456+
#endif
457+
458+
Local<Script> script;
459+
if (Script::Compile(context(), String::Empty(isolate())).ToLocal(&script))
460+
USE(script->Run(context()));
461+
462+
DCHECK(consistency_check);
463+
}
464+
441465
// FreeEnvironment() should have set this.
442466
CHECK(is_stopping());
443467

0 commit comments

Comments
 (0)