From 9fd2df73008561d447d7abb026d0de0e7360af44 Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Wed, 30 Mar 2022 20:00:09 +0900 Subject: [PATCH 1/2] src: handling v8 thread pool of zero fix: #42523 Problem: If no platform worker exists, Node.js doesn't shut down when background tasks exist. It keeps waiting in `NodePlatform::DrainTasks`. Observation: It seems that Node.js used to use V8's `DefaultPlatform` implementation, which chooses a suitable default value in case that `--v8-pool-size=0` is given as a command-line option. However, Node.js currently uses its own v8::Platform implementation, `NodePlatform`. It doesn't have the logic to handle the case. I referred to #4344 to track the issue. --- doc/api/cli.md | 4 +--- src/node_platform.cc | 15 +++++++++++++++ test/parallel/test-worker-v8-pool-size-0.js | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-worker-v8-pool-size-0.js diff --git a/doc/api/cli.md b/doc/api/cli.md index cec177f0410c8e..bd44b29dcca38a 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -1368,11 +1368,9 @@ added: v5.10.0 Set V8's thread pool size which will be used to allocate background jobs. -If set to `0` then V8 will choose an appropriate size of the thread pool based +If set to `0` then Node.js will choose an appropriate size of the thread pool based on the number of online processors. -If the value provided is larger than V8's maximum, then the largest value -will be chosen. ### `--zero-fill-buffers` diff --git a/src/node_platform.cc b/src/node_platform.cc index 9787cbb3edc2e2..4d7f16d15bc869 100644 --- a/src/node_platform.cc +++ b/src/node_platform.cc @@ -45,6 +45,19 @@ static void PlatformWorkerThread(void* data) { } } +static int GetActualThreadPoolSize(int thread_pool_size) { + if (thread_pool_size < 1) { + uv_cpu_info_t* cpu_info; + int count; + + if (uv_cpu_info(&cpu_info, &count) == 0) { + uv_free_cpu_info(cpu_info, count); + thread_pool_size = count - 1; + } + } + return std::max(thread_pool_size, 1); +} + } // namespace class WorkerThreadsTaskRunner::DelayedTaskScheduler { @@ -340,6 +353,8 @@ NodePlatform::NodePlatform(int thread_pool_size, // current v8::Platform instance. SetTracingController(tracing_controller_); DCHECK_EQ(GetTracingController(), tracing_controller_); + + thread_pool_size = GetActualThreadPoolSize(thread_pool_size); worker_thread_task_runner_ = std::make_shared(thread_pool_size); } diff --git a/test/parallel/test-worker-v8-pool-size-0.js b/test/parallel/test-worker-v8-pool-size-0.js new file mode 100644 index 00000000000000..1ba867f63f406c --- /dev/null +++ b/test/parallel/test-worker-v8-pool-size-0.js @@ -0,0 +1,15 @@ +// Flags: --v8-pool-size=0 +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const { Worker, isMainThread } = require('worker_threads'); + +if (isMainThread) { + const w = new Worker(__filename); + w.on('online', common.mustCall()); + w.on('exit', common.mustCall((code) => assert.strictEqual(code, 0))); + process.on('exit', (code) => { + assert.strictEqual(code, 0); + }); +} From 182a83ff0f26bd072d8da84a22badc89f34be9e8 Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Wed, 30 Mar 2022 20:18:05 +0900 Subject: [PATCH 2/2] doc: fix lint-md ci error --- doc/api/cli.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/doc/api/cli.md b/doc/api/cli.md index bd44b29dcca38a..4fd7aa1e7f5b8e 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -1368,9 +1368,8 @@ added: v5.10.0 Set V8's thread pool size which will be used to allocate background jobs. -If set to `0` then Node.js will choose an appropriate size of the thread pool based -on the number of online processors. - +If set to `0` then Node.js will choose an appropriate size of the thread pool +based on the number of online processors. ### `--zero-fill-buffers`