diff --git a/doc/api/cli.md b/doc/api/cli.md index cec177f0410c8e..4fd7aa1e7f5b8e 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -1368,11 +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 V8 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. +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` 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); + }); +}