@@ -163,14 +163,17 @@ bool AsyncHooks::pop_async_context(double async_id) {
163163}
164164
165165void  AsyncHooks::clear_async_id_stack () {
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 )));
166+   if  (env ()->can_call_into_js ()) {
167+     Isolate* isolate = env ()->isolate ();
168+     HandleScope handle_scope (isolate);
169+     if  (!js_execution_async_resources_.IsEmpty ()) {
170+       USE (PersistentToLocal::Strong (js_execution_async_resources_)
171+               ->Set (env ()->context (),
172+                     env ()->length_string (),
173+                     Integer::NewFromUnsigned (isolate, 0 )));
174+     }
173175  }
176+ 
174177  native_execution_async_resources_.clear ();
175178  native_execution_async_resources_.shrink_to_fit ();
176179
@@ -1068,7 +1071,13 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) {
10681071  TRACE_EVENT0 (TRACING_CATEGORY_NODE1 (environment),
10691072               " RunAndClearNativeImmediates"  );
10701073  HandleScope handle_scope (isolate_);
1071-   InternalCallbackScope cb_scope (this , Object::New (isolate_), { 0 , 0  });
1074+   //  In case the Isolate is no longer accessible just use an empty Local. This
1075+   //  is not an issue for InternalCallbackScope as this case is already handled
1076+   //  in its constructor but we avoid calls into v8 which can crash the process
1077+   //  in debug builds.
1078+   Local<Object> obj =
1079+       can_call_into_js () ? Object::New (isolate_) : Local<Object>();
1080+   InternalCallbackScope cb_scope (this , obj, {0 , 0 });
10721081
10731082  size_t  ref_count = 0 ;
10741083
0 commit comments