From 8c4ac6dcf5a58b320cd8e0a91118b2b38c0aca34 Mon Sep 17 00:00:00 2001 From: theanarkh Date: Sat, 29 Oct 2022 03:38:22 +0800 Subject: [PATCH] src: call uv_library_shutdown before DisposePlatform MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the process exits, there may be tasks in the thread pool that need to access data in the platform, such as trace agent. So make sure the thread pool exits first. see https://github.com/nodejs/node/pull/44458 PR-URL: https://github.com/nodejs/node/pull/45226 Refs: https://github.com/nodejs/node/pull/44458 Reviewed-By: Ben Noordhuis Reviewed-By: Santiago Gimeno Reviewed-By: Juan José Arboleda --- src/api/environment.cc | 7 ++++++- src/node_v8_platform-inl.h | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) 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;