diff --git a/src/api/environment.cc b/src/api/environment.cc index b8f2e41aeabc80..365dc5c51b29f8 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -779,8 +779,13 @@ void DefaultProcessExitHandler(Environment* env, int exit_code) { env->set_can_call_into_js(false); env->stop_sub_worker_contexts(); env->isolate()->DumpAndResetStats(); - DisposePlatform(); + // When the process exits, the tasks in the thread pool may also need to + // access the data of V8Platform, such as trace agent, or a field + // added in the future. So make sure the thread pool exits first. + // And make sure V8Platform don not call into Libuv threadpool, see Dispose + // in node_v8_platform-inl.h uv_library_shutdown(); + DisposePlatform(); exit(exit_code); } diff --git a/src/node_v8_platform-inl.h b/src/node_v8_platform-inl.h index d79dd99bf34da6..2504ff30b77e20 100644 --- a/src/node_v8_platform-inl.h +++ b/src/node_v8_platform-inl.h @@ -103,7 +103,8 @@ struct V8Platform { platform_ = new NodePlatform(thread_pool_size, controller); v8::V8::InitializePlatform(platform_); } - + // Make sure V8Platform don not call into Libuv threadpool, + // see DefaultProcessExitHandlerInternal in environment.cc inline void Dispose() { if (!initialized_) return;