Skip to content

Commit cf60597

Browse files
committed
worker: add name for worker
1 parent 049664b commit cf60597

File tree

11 files changed

+120
-5
lines changed

11 files changed

+120
-5
lines changed

doc/api/worker_threads.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,17 @@ An integer identifier for the current thread. On the corresponding worker object
721721
(if there is any), it is available as [`worker.threadId`][].
722722
This value is unique for each [`Worker`][] instance inside a single process.
723723
724+
## `worker.threadName`
725+
726+
<!-- YAML
727+
added: REPLACEME
728+
-->
729+
730+
* {string}
731+
732+
A string identifier for the current thread. On the corresponding worker object
733+
(if there is any), it is available as [`worker.threadName`][].
734+
724735
## `worker.workerData`
725736
726737
<!-- YAML
@@ -1855,6 +1866,17 @@ An integer identifier for the referenced thread. Inside the worker thread,
18551866
it is available as [`require('node:worker_threads').threadId`][].
18561867
This value is unique for each `Worker` instance inside a single process.
18571868

1869+
### `worker.threadName`
1870+
1871+
<!-- YAML
1872+
added: REPLACEME
1873+
-->
1874+
1875+
* {string}
1876+
1877+
A string identifier for the referenced thread. Inside the worker thread,
1878+
it is available as [`require('node:worker_threads').threadName`][].
1879+
18581880
### `worker.unref()`
18591881

18601882
<!-- YAML
@@ -1981,6 +2003,7 @@ thread spawned will spawn another until the application crashes.
19812003
[`require('node:worker_threads').parentPort.postMessage()`]: #workerpostmessagevalue-transferlist
19822004
[`require('node:worker_threads').parentPort`]: #workerparentport
19832005
[`require('node:worker_threads').threadId`]: #workerthreadid
2006+
[`require('node:worker_threads').threadName`]: #workerthreadname
19842007
[`require('node:worker_threads').workerData`]: #workerworkerdata
19852008
[`trace_events`]: tracing.md
19862009
[`v8.getHeapSnapshot()`]: v8.md#v8getheapsnapshotoptions
@@ -1991,6 +2014,7 @@ thread spawned will spawn another until the application crashes.
19912014
[`worker.postMessage()`]: #workerpostmessagevalue-transferlist
19922015
[`worker.terminate()`]: #workerterminate
19932016
[`worker.threadId`]: #workerthreadid_1
2017+
[`worker.threadName`]: #workerthreadname_1
19942018
[async-resource-worker-pool]: async_context.md#using-asyncresource-for-a-worker-thread-pool
19952019
[browser `MessagePort`]: https://developer.mozilla.org/en-US/docs/Web/API/MessagePort
19962020
[child processes]: child_process.md

lib/internal/worker.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const {
7171
isInternalThread,
7272
resourceLimits: resourceLimitsRaw,
7373
threadId,
74+
threadName,
7475
Worker: WorkerImpl,
7576
kMaxYoungGenerationSizeMb,
7677
kMaxOldGenerationSizeMb,
@@ -423,6 +424,12 @@ class Worker extends EventEmitter {
423424
return this[kHandle].threadId;
424425
}
425426

427+
get threadName() {
428+
if (this[kHandle] === null) return '';
429+
430+
return this[kHandle].threadName;
431+
}
432+
426433
get stdin() {
427434
return this[kParentSideStdio].stdin;
428435
}
@@ -556,6 +563,7 @@ module.exports = {
556563
getEnvironmentData,
557564
assignEnvironmentData,
558565
threadId,
566+
threadName,
559567
InternalWorker,
560568
Worker,
561569
};

lib/worker_threads.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
setEnvironmentData,
99
getEnvironmentData,
1010
threadId,
11+
threadName,
1112
Worker,
1213
} = require('internal/worker');
1314

@@ -42,6 +43,7 @@ module.exports = {
4243
resourceLimits,
4344
postMessageToThread,
4445
threadId,
46+
threadName,
4547
SHARE_ENV,
4648
Worker,
4749
parentPort: null,

src/api/environment.cc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,25 @@ Environment* CreateEnvironment(
414414
EnvironmentFlags::Flags flags,
415415
ThreadId thread_id,
416416
std::unique_ptr<InspectorParentHandle> inspector_parent_handle) {
417+
return CreateEnvironment(isolate_data,
418+
context,
419+
args,
420+
exec_args,
421+
flags,
422+
thread_id,
423+
std::move(inspector_parent_handle),
424+
{});
425+
}
426+
427+
Environment* CreateEnvironment(
428+
IsolateData* isolate_data,
429+
Local<Context> context,
430+
const std::vector<std::string>& args,
431+
const std::vector<std::string>& exec_args,
432+
EnvironmentFlags::Flags flags,
433+
ThreadId thread_id,
434+
std::unique_ptr<InspectorParentHandle> inspector_parent_handle,
435+
std::string_view thread_name) {
417436
Isolate* isolate = isolate_data->isolate();
418437

419438
Isolate::Scope isolate_scope(isolate);
@@ -434,7 +453,8 @@ Environment* CreateEnvironment(
434453
exec_args,
435454
env_snapshot_info,
436455
flags,
437-
thread_id);
456+
thread_id,
457+
thread_name);
438458
CHECK_NOT_NULL(env);
439459

440460
if (use_snapshot) {

src/env-inl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,10 @@ inline uint64_t Environment::thread_id() const {
695695
return thread_id_;
696696
}
697697

698+
inline std::string_view Environment::thread_name() const {
699+
return thread_name_;
700+
}
701+
698702
inline worker::Worker* Environment::worker_context() const {
699703
return isolate_data()->worker_context();
700704
}

src/env.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,8 @@ Environment::Environment(IsolateData* isolate_data,
784784
const std::vector<std::string>& exec_args,
785785
const EnvSerializeInfo* env_info,
786786
EnvironmentFlags::Flags flags,
787-
ThreadId thread_id)
787+
ThreadId thread_id,
788+
std::string_view thread_name)
788789
: isolate_(isolate),
789790
external_memory_accounter_(new ExternalMemoryAccounter()),
790791
isolate_data_(isolate_data),
@@ -811,7 +812,8 @@ Environment::Environment(IsolateData* isolate_data,
811812
flags_(flags),
812813
thread_id_(thread_id.id == static_cast<uint64_t>(-1)
813814
? AllocateEnvironmentThreadId().id
814-
: thread_id.id) {
815+
: thread_id.id),
816+
thread_name_(thread_name) {
815817
if (!is_main_thread()) {
816818
// If this is a Worker thread, we can always safely use the parent's
817819
// Isolate's code cache because of the shared read-only heap.

src/env.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,8 @@ class Environment final : public MemoryRetainer {
660660
const std::vector<std::string>& exec_args,
661661
const EnvSerializeInfo* env_info,
662662
EnvironmentFlags::Flags flags,
663-
ThreadId thread_id);
663+
ThreadId thread_id,
664+
std::string_view thread_name = "");
664665
void InitializeMainContext(v8::Local<v8::Context> context,
665666
const EnvSerializeInfo* env_info);
666667
~Environment() override;
@@ -807,6 +808,7 @@ class Environment final : public MemoryRetainer {
807808
inline bool should_start_debug_signal_handler() const;
808809
inline bool no_browser_globals() const;
809810
inline uint64_t thread_id() const;
811+
inline std::string_view thread_name() const;
810812
inline worker::Worker* worker_context() const;
811813
Environment* worker_parent_env() const;
812814
inline void add_sub_worker_context(worker::Worker* context);
@@ -1172,6 +1174,7 @@ class Environment final : public MemoryRetainer {
11721174

11731175
uint64_t flags_;
11741176
uint64_t thread_id_;
1177+
std::string thread_name_;
11751178
std::unordered_set<worker::Worker*> sub_worker_contexts_;
11761179

11771180
#if HAVE_INSPECTOR

src/env_properties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@
386386
V(table_string, "table") \
387387
V(target_string, "target") \
388388
V(thread_id_string, "threadId") \
389+
V(thread_name_string, "threadName") \
389390
V(ticketkeycallback_string, "onticketkeycallback") \
390391
V(timeout_string, "timeout") \
391392
V(time_to_first_byte_string, "timeToFirstByte") \

src/node.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,16 @@ NODE_EXTERN Environment* CreateEnvironment(
685685
ThreadId thread_id = {} /* allocates a thread id automatically */,
686686
std::unique_ptr<InspectorParentHandle> inspector_parent_handle = {});
687687

688+
NODE_EXTERN Environment* CreateEnvironment(
689+
IsolateData* isolate_data,
690+
v8::Local<v8::Context> context,
691+
const std::vector<std::string>& args,
692+
const std::vector<std::string>& exec_args,
693+
EnvironmentFlags::Flags flags,
694+
ThreadId thread_id,
695+
std::unique_ptr<InspectorParentHandle> inspector_parent_handle,
696+
std::string_view thread_name);
697+
688698
// Returns a handle that can be passed to `LoadEnvironment()`, making the
689699
// child Environment accessible to the inspector as if it were a Node.js Worker.
690700
// `child_thread_id` can be created using `AllocateEnvironmentThreadId()`

src/node_worker.cc

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ using v8::Isolate;
3232
using v8::Local;
3333
using v8::Locker;
3434
using v8::Maybe;
35+
using v8::NewStringType;
3536
using v8::Null;
3637
using v8::Number;
3738
using v8::Object;
@@ -88,6 +89,15 @@ Worker::Worker(Environment* env,
8889
Number::New(env->isolate(), static_cast<double>(thread_id_.id)))
8990
.Check();
9091

92+
object()
93+
->Set(env->context(),
94+
env->thread_name_string(),
95+
String::NewFromUtf8(env->isolate(),
96+
name_.data(),
97+
NewStringType::kNormal,
98+
name_.size())
99+
.ToLocalChecked())
100+
.Check();
91101
// Without this check, to use the permission model with
92102
// workers (--allow-worker) one would need to pass --allow-inspector as well
93103
if (env->permission()->is_granted(
@@ -364,7 +374,8 @@ void Worker::Run() {
364374
std::move(exec_argv_),
365375
static_cast<EnvironmentFlags::Flags>(environment_flags_),
366376
thread_id_,
367-
std::move(inspector_parent_handle_)));
377+
std::move(inspector_parent_handle_),
378+
name_));
368379
if (is_stopped()) return;
369380
CHECK_NOT_NULL(env_);
370381
env_->set_env_vars(std::move(env_vars_));
@@ -1149,6 +1160,16 @@ void CreateWorkerPerContextProperties(Local<Object> target,
11491160
Number::New(isolate, static_cast<double>(env->thread_id())))
11501161
.Check();
11511162

1163+
target
1164+
->Set(env->context(),
1165+
env->thread_name_string(),
1166+
String::NewFromUtf8(isolate,
1167+
env->thread_name().data(),
1168+
NewStringType::kNormal,
1169+
env->thread_name().size())
1170+
.ToLocalChecked())
1171+
.Check();
1172+
11521173
target
11531174
->Set(env->context(),
11541175
FIXED_ONE_BYTE_STRING(isolate, "isMainThread"),

0 commit comments

Comments
 (0)