Skip to content

Commit eecd327

Browse files
theanarkhRafaelGSS
authored andcommitted
worker: add name for worker
PR-URL: #59213 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 8686b80 commit eecd327

File tree

11 files changed

+118
-5
lines changed

11 files changed

+118
-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|null}
731+
732+
A string identifier for the current thread or null if the thread is not running.
733+
On the corresponding worker object (if there is any), it is available as [`worker.threadName`][].
734+
724735
## `worker.workerData`
725736
726737
<!-- YAML
@@ -2011,6 +2022,17 @@ An integer identifier for the referenced thread. Inside the worker thread,
20112022
it is available as [`require('node:worker_threads').threadId`][].
20122023
This value is unique for each `Worker` instance inside a single process.
20132024

2025+
### `worker.threadName`
2026+
2027+
<!-- YAML
2028+
added: REPLACEME
2029+
-->
2030+
2031+
* {string|null}
2032+
2033+
A string identifier for the referenced thread or null if the thread is not running.
2034+
Inside the worker thread, it is available as [`require('node:worker_threads').threadName`][].
2035+
20142036
### `worker.unref()`
20152037

20162038
<!-- YAML
@@ -2139,6 +2161,7 @@ thread spawned will spawn another until the application crashes.
21392161
[`require('node:worker_threads').parentPort.postMessage()`]: #workerpostmessagevalue-transferlist
21402162
[`require('node:worker_threads').parentPort`]: #workerparentport
21412163
[`require('node:worker_threads').threadId`]: #workerthreadid
2164+
[`require('node:worker_threads').threadName`]: #workerthreadname
21422165
[`require('node:worker_threads').workerData`]: #workerworkerdata
21432166
[`trace_events`]: tracing.md
21442167
[`v8.getHeapSnapshot()`]: v8.md#v8getheapsnapshotoptions
@@ -2149,6 +2172,7 @@ thread spawned will spawn another until the application crashes.
21492172
[`worker.postMessage()`]: #workerpostmessagevalue-transferlist
21502173
[`worker.terminate()`]: #workerterminate
21512174
[`worker.threadId`]: #workerthreadid_1
2175+
[`worker.threadName`]: #workerthreadname_1
21522176
[async-resource-worker-pool]: async_context.md#using-asyncresource-for-a-worker-thread-pool
21532177
[browser `LockManager`]: https://developer.mozilla.org/en-US/docs/Web/API/LockManager
21542178
[browser `MessagePort`]: https://developer.mozilla.org/en-US/docs/Web/API/MessagePort

lib/internal/worker.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const {
7272
isInternalThread,
7373
resourceLimits: resourceLimitsRaw,
7474
threadId,
75+
threadName,
7576
Worker: WorkerImpl,
7677
kMaxYoungGenerationSizeMb,
7778
kMaxOldGenerationSizeMb,
@@ -433,6 +434,12 @@ class Worker extends EventEmitter {
433434
return this[kHandle].threadId;
434435
}
435436

437+
get threadName() {
438+
if (this[kHandle] === null) return null;
439+
440+
return this[kHandle].threadName;
441+
}
442+
436443
get stdin() {
437444
return this[kParentSideStdio].stdin;
438445
}
@@ -597,6 +604,7 @@ module.exports = {
597604
getEnvironmentData,
598605
assignEnvironmentData,
599606
threadId,
607+
threadName,
600608
InternalWorker,
601609
Worker,
602610
};

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

@@ -44,6 +45,7 @@ module.exports = {
4445
resourceLimits,
4546
postMessageToThread,
4647
threadId,
48+
threadName,
4749
SHARE_ENV,
4850
Worker,
4951
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
@@ -703,6 +703,10 @@ inline uint64_t Environment::thread_id() const {
703703
return thread_id_;
704704
}
705705

706+
inline std::string_view Environment::thread_name() const {
707+
return thread_name_;
708+
}
709+
706710
inline worker::Worker* Environment::worker_context() const {
707711
return isolate_data()->worker_context();
708712
}

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);
@@ -1176,6 +1178,7 @@ class Environment final : public MemoryRetainer {
11761178

11771179
uint64_t flags_;
11781180
uint64_t thread_id_;
1181+
std::string thread_name_;
11791182
std::unordered_set<worker::Worker*> sub_worker_contexts_;
11801183

11811184
#if HAVE_INSPECTOR

src/env_properties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@
390390
V(table_string, "table") \
391391
V(target_string, "target") \
392392
V(thread_id_string, "threadId") \
393+
V(thread_name_string, "threadName") \
393394
V(ticketkeycallback_string, "onticketkeycallback") \
394395
V(timeout_string, "timeout") \
395396
V(time_to_first_byte_string, "timeToFirstByte") \

src/node.h

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

733+
NODE_EXTERN Environment* CreateEnvironment(
734+
IsolateData* isolate_data,
735+
v8::Local<v8::Context> context,
736+
const std::vector<std::string>& args,
737+
const std::vector<std::string>& exec_args,
738+
EnvironmentFlags::Flags flags,
739+
ThreadId thread_id,
740+
std::unique_ptr<InspectorParentHandle> inspector_parent_handle,
741+
std::string_view thread_name);
742+
733743
// Returns a handle that can be passed to `LoadEnvironment()`, making the
734744
// child Environment accessible to the inspector as if it were a Node.js Worker.
735745
// `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
@@ -33,6 +33,7 @@ using v8::Local;
3333
using v8::Locker;
3434
using v8::Maybe;
3535
using v8::Name;
36+
using v8::NewStringType;
3637
using v8::Null;
3738
using v8::Number;
3839
using v8::Object;
@@ -89,6 +90,15 @@ Worker::Worker(Environment* env,
8990
Number::New(env->isolate(), static_cast<double>(thread_id_.id)))
9091
.Check();
9192

93+
object()
94+
->Set(env->context(),
95+
env->thread_name_string(),
96+
String::NewFromUtf8(env->isolate(),
97+
name_.data(),
98+
NewStringType::kNormal,
99+
name_.size())
100+
.ToLocalChecked())
101+
.Check();
92102
// Without this check, to use the permission model with
93103
// workers (--allow-worker) one would need to pass --allow-inspector as well
94104
if (env->permission()->is_granted(
@@ -365,7 +375,8 @@ void Worker::Run() {
365375
std::move(exec_argv_),
366376
static_cast<EnvironmentFlags::Flags>(environment_flags_),
367377
thread_id_,
368-
std::move(inspector_parent_handle_)));
378+
std::move(inspector_parent_handle_),
379+
name_));
369380
if (is_stopped()) return;
370381
CHECK_NOT_NULL(env_);
371382
env_->set_env_vars(std::move(env_vars_));
@@ -1239,6 +1250,16 @@ void CreateWorkerPerContextProperties(Local<Object> target,
12391250
Number::New(isolate, static_cast<double>(env->thread_id())))
12401251
.Check();
12411252

1253+
target
1254+
->Set(env->context(),
1255+
env->thread_name_string(),
1256+
String::NewFromUtf8(isolate,
1257+
env->thread_name().data(),
1258+
NewStringType::kNormal,
1259+
env->thread_name().size())
1260+
.ToLocalChecked())
1261+
.Check();
1262+
12421263
target
12431264
->Set(env->context(),
12441265
FIXED_ONE_BYTE_STRING(isolate, "isMainThread"),

0 commit comments

Comments
 (0)