@@ -162,14 +162,17 @@ bool AsyncHooks::pop_async_context(double async_id) {
162162}
163163
164164void  AsyncHooks::clear_async_id_stack () {
165-   Isolate* isolate = env ()->isolate ();
166-   HandleScope handle_scope (isolate);
167-   if  (!js_execution_async_resources_.IsEmpty ()) {
168-     USE (PersistentToLocal::Strong (js_execution_async_resources_)
169-             ->Set (env ()->context (),
170-                   env ()->length_string (),
171-                   Integer::NewFromUnsigned (isolate, 0 )));
165+   if  (env ()->can_call_into_js ()) {
166+     Isolate* isolate = env ()->isolate ();
167+     HandleScope handle_scope (isolate);
168+     if  (!js_execution_async_resources_.IsEmpty ()) {
169+       USE (PersistentToLocal::Strong (js_execution_async_resources_)
170+               ->Set (env ()->context (),
171+                     env ()->length_string (),
172+                     Integer::NewFromUnsigned (isolate, 0 )));
173+     }
172174  }
175+ 
173176  native_execution_async_resources_.clear ();
174177  native_execution_async_resources_.shrink_to_fit ();
175178
@@ -1157,7 +1160,13 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) {
11571160  TRACE_EVENT0 (TRACING_CATEGORY_NODE1 (environment),
11581161               " RunAndClearNativeImmediates"  );
11591162  HandleScope handle_scope (isolate_);
1160-   InternalCallbackScope cb_scope (this , Object::New (isolate_), { 0 , 0  });
1163+   //  In case the Isolate is no longer accessible just use an empty Local. This
1164+   //  is not an issue for InternalCallbackScope as this case is already handled
1165+   //  in its constructor but we avoid calls into v8 which can crash the process
1166+   //  in debug builds.
1167+   Local<Object> obj =
1168+       can_call_into_js () ? Object::New (isolate_) : Local<Object>();
1169+   InternalCallbackScope cb_scope (this , obj, {0 , 0 });
11611170
11621171  size_t  ref_count = 0 ;
11631172
0 commit comments