Skip to content

Commit 3090def

Browse files
authored
worker: add name for worker
PR-URL: #59213 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent a4ce69e commit 3090def

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
@@ -2015,6 +2026,17 @@ An integer identifier for the referenced thread. Inside the worker thread,
20152026
it is available as [`require('node:worker_threads').threadId`][].
20162027
This value is unique for each `Worker` instance inside a single process.
20172028

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

20202042
<!-- YAML
@@ -2145,6 +2167,7 @@ thread spawned will spawn another until the application crashes.
21452167
[`require('node:worker_threads').parentPort.postMessage()`]: #workerpostmessagevalue-transferlist
21462168
[`require('node:worker_threads').parentPort`]: #workerparentport
21472169
[`require('node:worker_threads').threadId`]: #workerthreadid
2170+
[`require('node:worker_threads').threadName`]: #workerthreadname
21482171
[`require('node:worker_threads').workerData`]: #workerworkerdata
21492172
[`trace_events`]: tracing.md
21502173
[`v8.getHeapSnapshot()`]: v8.md#v8getheapsnapshotoptions
@@ -2155,6 +2178,7 @@ thread spawned will spawn another until the application crashes.
21552178
[`worker.postMessage()`]: #workerpostmessagevalue-transferlist
21562179
[`worker.terminate()`]: #workerterminate
21572180
[`worker.threadId`]: #workerthreadid_1
2181+
[`worker.threadName`]: #workerthreadname_1
21582182
[async-resource-worker-pool]: async_context.md#using-asyncresource-for-a-worker-thread-pool
21592183
[browser `LockManager`]: https://developer.mozilla.org/en-US/docs/Web/API/LockManager
21602184
[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
@@ -73,6 +73,7 @@ const {
7373
isInternalThread,
7474
resourceLimits: resourceLimitsRaw,
7575
threadId,
76+
threadName,
7677
Worker: WorkerImpl,
7778
kMaxYoungGenerationSizeMb,
7879
kMaxOldGenerationSizeMb,
@@ -425,6 +426,12 @@ class Worker extends EventEmitter {
425426
return this[kHandle].threadId;
426427
}
427428

429+
get threadName() {
430+
if (this[kHandle] === null) return null;
431+
432+
return this[kHandle].threadName;
433+
}
434+
428435
get stdin() {
429436
return this[kParentSideStdio].stdin;
430437
}
@@ -589,6 +596,7 @@ module.exports = {
589596
getEnvironmentData,
590597
assignEnvironmentData,
591598
threadId,
599+
threadName,
592600
InternalWorker,
593601
Worker,
594602
};

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
@@ -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
@@ -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
@@ -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
@@ -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)