diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index 4e63345d20..da6883e9d4 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -1229,9 +1229,16 @@ changes: used for generated code. * `stackSizeMb` {number} The default maximum stack size for the thread. Small values may lead to unusable Worker instances. **Default:** `4`. - * `name` {string} An optional `name` to be appended to the worker title - for debugging/identification purposes, making the final title as - `[worker ${id}] ${name}`. **Default:** `''`. + * `name` {string} An optional `name` to be replaced in the thread name + and to the worker title for debugging/identification purposes, + making the final title as `[worker ${id}] ${name}`. + This parameter has a maximum allowed size, depending on the operating + system. If the provided name exceeds the limit, it will be truncated + * Maximum sizes: + * Windows: 32,767 characters + * macOS: 64 characters + * Other systems: 16 characters + **Default:** `'WorkerThread'`. ### Event: `'error'` diff --git a/lib/internal/worker.js b/lib/internal/worker.js index e5a2cd0689..39ca1778c7 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -204,7 +204,7 @@ class Worker extends EventEmitter { options.env); } - let name = ''; + let name = 'WorkerThread'; if (options.name) { validateString(options.name, 'options.name'); name = StringPrototypeTrim(options.name); diff --git a/src/node_worker.cc b/src/node_worker.cc index 1fc3774948..edb43bc796 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -740,6 +740,7 @@ void Worker::StartThread(const FunctionCallbackInfo& args) { Worker* w = static_cast(arg); const uintptr_t stack_top = reinterpret_cast(&arg); + uv_thread_setname(w->name_.c_str()); // Leave a few kilobytes just to make sure we're within limits and have // some space to do work in C++ land. w->stack_base_ = stack_top - (w->stack_size_ - kStackBufferSize); diff --git a/test/parallel/test-trace-events-worker-metadata.js b/test/parallel/test-trace-events-worker-metadata.js index 6a8702ccad..844b3769ce 100644 --- a/test/parallel/test-trace-events-worker-metadata.js +++ b/test/parallel/test-trace-events-worker-metadata.js @@ -23,7 +23,7 @@ if (isMainThread) { assert(traces.length > 0); assert(traces.some((trace) => trace.cat === '__metadata' && trace.name === 'thread_name' && - trace.args.name === '[worker 1]')); + trace.args.name === '[worker 1] WorkerThread')); })); })); } else {