Skip to content

Commit 98cd7e2

Browse files
theanarkhrichardlau
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 8e0f9cd commit 98cd7e2

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
@@ -715,6 +715,17 @@ An integer identifier for the current thread. On the corresponding worker object
715715
(if there is any), it is available as [`worker.threadId`][].
716716
This value is unique for each [`Worker`][] instance inside a single process.
717717
718+
## `worker.threadName`
719+
720+
<!-- YAML
721+
added: REPLACEME
722+
-->
723+
724+
* {string|null}
725+
726+
A string identifier for the current thread or null if the thread is not running.
727+
On the corresponding worker object (if there is any), it is available as [`worker.threadName`][].
728+
718729
## `worker.workerData`
719730
720731
<!-- YAML
@@ -1849,6 +1860,17 @@ An integer identifier for the referenced thread. Inside the worker thread,
18491860
it is available as [`require('node:worker_threads').threadId`][].
18501861
This value is unique for each `Worker` instance inside a single process.
18511862

1863+
### `worker.threadName`
1864+
1865+
<!-- YAML
1866+
added: REPLACEME
1867+
-->
1868+
1869+
* {string|null}
1870+
1871+
A string identifier for the referenced thread or null if the thread is not running.
1872+
Inside the worker thread, it is available as [`require('node:worker_threads').threadName`][].
1873+
18521874
### `worker.unref()`
18531875

18541876
<!-- YAML
@@ -1969,6 +1991,7 @@ thread spawned will spawn another until the application crashes.
19691991
[`require('node:worker_threads').parentPort.postMessage()`]: #workerpostmessagevalue-transferlist
19701992
[`require('node:worker_threads').parentPort`]: #workerparentport
19711993
[`require('node:worker_threads').threadId`]: #workerthreadid
1994+
[`require('node:worker_threads').threadName`]: #workerthreadname
19721995
[`require('node:worker_threads').workerData`]: #workerworkerdata
19731996
[`trace_events`]: tracing.md
19741997
[`v8.getHeapSnapshot()`]: v8.md#v8getheapsnapshotoptions
@@ -1979,6 +2002,7 @@ thread spawned will spawn another until the application crashes.
19792002
[`worker.postMessage()`]: #workerpostmessagevalue-transferlist
19802003
[`worker.terminate()`]: #workerterminate
19812004
[`worker.threadId`]: #workerthreadid_1
2005+
[`worker.threadName`]: #workerthreadname_1
19822006
[async-resource-worker-pool]: async_context.md#using-asyncresource-for-a-worker-thread-pool
19832007
[browser `MessagePort`]: https://developer.mozilla.org/en-US/docs/Web/API/MessagePort
19842008
[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,
@@ -432,6 +433,12 @@ class Worker extends EventEmitter {
432433
return this[kHandle].threadId;
433434
}
434435

436+
get threadName() {
437+
if (this[kHandle] === null) return null;
438+
439+
return this[kHandle].threadName;
440+
}
441+
435442
get stdin() {
436443
return this[kParentSideStdio].stdin;
437444
}
@@ -596,6 +603,7 @@ module.exports = {
596603
getEnvironmentData,
597604
assignEnvironmentData,
598605
threadId,
606+
threadName,
599607
InternalWorker,
600608
Worker,
601609
};

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
@@ -405,6 +405,25 @@ Environment* CreateEnvironment(
405405
EnvironmentFlags::Flags flags,
406406
ThreadId thread_id,
407407
std::unique_ptr<InspectorParentHandle> inspector_parent_handle) {
408+
return CreateEnvironment(isolate_data,
409+
context,
410+
args,
411+
exec_args,
412+
flags,
413+
thread_id,
414+
std::move(inspector_parent_handle),
415+
{});
416+
}
417+
418+
Environment* CreateEnvironment(
419+
IsolateData* isolate_data,
420+
Local<Context> context,
421+
const std::vector<std::string>& args,
422+
const std::vector<std::string>& exec_args,
423+
EnvironmentFlags::Flags flags,
424+
ThreadId thread_id,
425+
std::unique_ptr<InspectorParentHandle> inspector_parent_handle,
426+
std::string_view thread_name) {
408427
Isolate* isolate = isolate_data->isolate();
409428

410429
Isolate::Scope isolate_scope(isolate);
@@ -425,7 +444,8 @@ Environment* CreateEnvironment(
425444
exec_args,
426445
env_snapshot_info,
427446
flags,
428-
thread_id);
447+
thread_id,
448+
thread_name);
429449
CHECK_NOT_NULL(env);
430450

431451
if (use_snapshot) {

src/env-inl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,10 @@ inline uint64_t Environment::thread_id() const {
729729
return thread_id_;
730730
}
731731

732+
inline std::string_view Environment::thread_name() const {
733+
return thread_name_;
734+
}
735+
732736
inline worker::Worker* Environment::worker_context() const {
733737
return isolate_data()->worker_context();
734738
}

src/env.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,8 @@ Environment::Environment(IsolateData* isolate_data,
803803
const std::vector<std::string>& exec_args,
804804
const EnvSerializeInfo* env_info,
805805
EnvironmentFlags::Flags flags,
806-
ThreadId thread_id)
806+
ThreadId thread_id,
807+
std::string_view thread_name)
807808
: isolate_(isolate),
808809
isolate_data_(isolate_data),
809810
async_hooks_(isolate, MAYBE_FIELD_PTR(env_info, async_hooks)),
@@ -829,7 +830,8 @@ Environment::Environment(IsolateData* isolate_data,
829830
flags_(flags),
830831
thread_id_(thread_id.id == static_cast<uint64_t>(-1)
831832
? AllocateEnvironmentThreadId().id
832-
: thread_id.id) {
833+
: thread_id.id),
834+
thread_name_(thread_name) {
833835
constexpr bool is_shared_ro_heap =
834836
#ifdef NODE_V8_SHARED_RO_HEAP
835837
true;

src/env.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,8 @@ class Environment final : public MemoryRetainer {
681681
const std::vector<std::string>& exec_args,
682682
const EnvSerializeInfo* env_info,
683683
EnvironmentFlags::Flags flags,
684-
ThreadId thread_id);
684+
ThreadId thread_id,
685+
std::string_view thread_name = "");
685686
void InitializeMainContext(v8::Local<v8::Context> context,
686687
const EnvSerializeInfo* env_info);
687688
~Environment() override;
@@ -826,6 +827,7 @@ class Environment final : public MemoryRetainer {
826827
inline bool should_start_debug_signal_handler() const;
827828
inline bool no_browser_globals() const;
828829
inline uint64_t thread_id() const;
830+
inline std::string_view thread_name() const;
829831
inline worker::Worker* worker_context() const;
830832
Environment* worker_parent_env() const;
831833
inline void add_sub_worker_context(worker::Worker* context);
@@ -1201,6 +1203,7 @@ class Environment final : public MemoryRetainer {
12011203

12021204
uint64_t flags_;
12031205
uint64_t thread_id_;
1206+
std::string thread_name_;
12041207
std::unordered_set<worker::Worker*> sub_worker_contexts_;
12051208

12061209
#if HAVE_INSPECTOR

src/env_properties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@
377377
V(table_string, "table") \
378378
V(target_string, "target") \
379379
V(thread_id_string, "threadId") \
380+
V(thread_name_string, "threadName") \
380381
V(ticketkeycallback_string, "onticketkeycallback") \
381382
V(timeout_string, "timeout") \
382383
V(time_to_first_byte_string, "timeToFirstByte") \

src/node.h

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

719+
NODE_EXTERN Environment* CreateEnvironment(
720+
IsolateData* isolate_data,
721+
v8::Local<v8::Context> context,
722+
const std::vector<std::string>& args,
723+
const std::vector<std::string>& exec_args,
724+
EnvironmentFlags::Flags flags,
725+
ThreadId thread_id,
726+
std::unique_ptr<InspectorParentHandle> inspector_parent_handle,
727+
std::string_view thread_name);
728+
719729
// Returns a handle that can be passed to `LoadEnvironment()`, making the
720730
// child Environment accessible to the inspector as if it were a Node.js Worker.
721731
// `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(
@@ -371,7 +381,8 @@ void Worker::Run() {
371381
std::move(exec_argv_),
372382
static_cast<EnvironmentFlags::Flags>(environment_flags_),
373383
thread_id_,
374-
std::move(inspector_parent_handle_)));
384+
std::move(inspector_parent_handle_),
385+
name_));
375386
if (is_stopped()) return;
376387
CHECK_NOT_NULL(env_);
377388
env_->set_env_vars(std::move(env_vars_));
@@ -1240,6 +1251,16 @@ void CreateWorkerPerContextProperties(Local<Object> target,
12401251
Number::New(isolate, static_cast<double>(env->thread_id())))
12411252
.Check();
12421253

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

0 commit comments

Comments
 (0)