Skip to content

Commit 3579143

Browse files
RafaelGSStargos
authored andcommitted
src: set worker thread name using worker.name
Set the worker thread name using worker.name value and changing the default to "WorkerThread" PR-URL: #56416 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]>
1 parent 736ff5d commit 3579143

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

doc/api/worker_threads.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -1227,9 +1227,16 @@ changes:
12271227
used for generated code.
12281228
* `stackSizeMb` {number} The default maximum stack size for the thread.
12291229
Small values may lead to unusable Worker instances. **Default:** `4`.
1230-
* `name` {string} An optional `name` to be appended to the worker title
1231-
for debugging/identification purposes, making the final title as
1232-
`[worker ${id}] ${name}`. **Default:** `''`.
1230+
* `name` {string} An optional `name` to be replaced in the thread name
1231+
and to the worker title for debugging/identification purposes,
1232+
making the final title as `[worker ${id}] ${name}`.
1233+
This parameter has a maximum allowed size, depending on the operating
1234+
system. If the provided name exceeds the limit, it will be truncated
1235+
* Maximum sizes:
1236+
* Windows: 32,767 characters
1237+
* macOS: 64 characters
1238+
* Other systems: 16 characters
1239+
**Default:** `'WorkerThread'`.
12331240

12341241
### Event: `'error'`
12351242

lib/internal/worker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class Worker extends EventEmitter {
204204
options.env);
205205
}
206206

207-
let name = '';
207+
let name = 'WorkerThread';
208208
if (options.name) {
209209
validateString(options.name, 'options.name');
210210
name = StringPrototypeTrim(options.name);

src/node_worker.cc

+1
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
740740
Worker* w = static_cast<Worker*>(arg);
741741
const uintptr_t stack_top = reinterpret_cast<uintptr_t>(&arg);
742742

743+
uv_thread_setname(w->name_.c_str());
743744
// Leave a few kilobytes just to make sure we're within limits and have
744745
// some space to do work in C++ land.
745746
w->stack_base_ = stack_top - (w->stack_size_ - kStackBufferSize);

test/parallel/test-trace-events-worker-metadata.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ if (isMainThread) {
2323
assert(traces.length > 0);
2424
assert(traces.some((trace) =>
2525
trace.cat === '__metadata' && trace.name === 'thread_name' &&
26-
trace.args.name === '[worker 1]'));
26+
trace.args.name === '[worker 1] WorkerThread'));
2727
}));
2828
}));
2929
} else {

0 commit comments

Comments
 (0)