@@ -41,11 +41,13 @@ using v8::NewStringType;
4141using v8::Number;
4242using v8::Object;
4343using v8::Private;
44+ using v8::Script;
4445using v8::SnapshotCreator;
4546using v8::StackTrace;
4647using v8::String;
4748using v8::Symbol;
4849using v8::TracingController;
50+ using v8::TryCatch;
4951using v8::Undefined;
5052using v8::Value;
5153using worker::Worker;
@@ -412,11 +414,30 @@ Environment::Environment(IsolateData* isolate_data,
412414}
413415
414416Environment::~Environment () {
415- if (Environment** interrupt_data = interrupt_data_.load ())
417+ if (Environment** interrupt_data = interrupt_data_.load ()) {
418+ // There are pending RequestInterrupt() callbacks. Tell them not to run,
419+ // then force V8 to run interrupts by compiling and running an empty script
420+ // so as not to leak memory.
416421 *interrupt_data = nullptr ;
417422
418- // FreeEnvironment() should have set this.
419- CHECK (is_stopping ());
423+ Isolate::AllowJavascriptExecutionScope allow_js_here (isolate ());
424+ HandleScope handle_scope (isolate ());
425+ TryCatch try_catch (isolate ());
426+ Context::Scope context_scope (context ());
427+
428+ #ifdef DEBUG
429+ bool consistency_check = false ;
430+ isolate ()->RequestInterrupt ([](Isolate*, void * data) {
431+ *static_cast <bool *>(data) = true ;
432+ }, &consistency_check);
433+ #endif
434+
435+ Local<Script> script;
436+ if (Script::Compile (context (), String::Empty (isolate ())).ToLocal (&script))
437+ USE (script->Run (context ()));
438+
439+ DCHECK (consistency_check);
440+ }
420441
421442 isolate ()->GetHeapProfiler ()->RemoveBuildEmbedderGraphCallback (
422443 BuildEmbedderGraph, this );
0 commit comments