@@ -42,11 +42,13 @@ using v8::NewStringType;
4242using v8::Number;
4343using v8::Object;
4444using v8::Private;
45+ using v8::Script;
4546using v8::SnapshotCreator;
4647using v8::StackTrace;
4748using v8::String;
4849using v8::Symbol;
4950using v8::TracingController;
51+ using v8::TryCatch;
5052using v8::Undefined;
5153using v8::Value;
5254using worker::Worker;
@@ -394,9 +396,31 @@ Environment::Environment(IsolateData* isolate_data,
394396}
395397
396398Environment::~Environment () {
397- if (Environment** interrupt_data = interrupt_data_.load ())
399+ if (Environment** interrupt_data = interrupt_data_.load ()) {
400+ // There are pending RequestInterrupt() callbacks. Tell them not to run,
401+ // then force V8 to run interrupts by compiling and running an empty script
402+ // so as not to leak memory.
398403 *interrupt_data = nullptr ;
399404
405+ Isolate::AllowJavascriptExecutionScope allow_js_here (isolate ());
406+ HandleScope handle_scope (isolate ());
407+ TryCatch try_catch (isolate ());
408+ Context::Scope context_scope (context ());
409+
410+ #ifdef DEBUG
411+ bool consistency_check = false ;
412+ isolate ()->RequestInterrupt ([](Isolate*, void * data) {
413+ *static_cast <bool *>(data) = true ;
414+ }, &consistency_check);
415+ #endif
416+
417+ Local<Script> script;
418+ if (Script::Compile (context (), String::Empty (isolate ())).ToLocal (&script))
419+ USE (script->Run (context ()));
420+
421+ DCHECK (consistency_check);
422+ }
423+
400424 // FreeEnvironment() should have set this.
401425 CHECK (is_stopping ());
402426
0 commit comments