@@ -50,6 +50,7 @@ constexpr double kMB = 1024 * 1024;
5050Worker::Worker (Environment* env,
5151 Local<Object> wrap,
5252 const std::string& url,
53+ const std::string& name,
5354 std::shared_ptr<PerIsolateOptions> per_isolate_opts,
5455 std::vector<std::string>&& exec_argv,
5556 std::shared_ptr<KVStore> env_vars,
@@ -59,6 +60,7 @@ Worker::Worker(Environment* env,
5960 exec_argv_(exec_argv),
6061 platform_(env->isolate_data ()->platform()),
6162 thread_id_(AllocateEnvironmentThreadId()),
63+ name_(name),
6264 env_vars_(env_vars),
6365 snapshot_data_(snapshot_data) {
6466 Debug (this , " Creating new worker instance with thread id %llu" ,
@@ -83,8 +85,8 @@ Worker::Worker(Environment* env,
8385 Number::New (env->isolate (), static_cast <double >(thread_id_.id )))
8486 .Check ();
8587
86- inspector_parent_handle_ = GetInspectorParentHandle (
87- env, thread_id_, url.c_str ());
88+ inspector_parent_handle_ =
89+ GetInspectorParentHandle ( env, thread_id_, url. c_str (), name .c_str ());
8890
8991 argv_ = std::vector<std::string>{env->argv ()[0 ]};
9092 // Mark this Worker object as weak until we actually start the thread.
@@ -265,11 +267,10 @@ size_t Worker::NearHeapLimit(void* data, size_t current_heap_limit,
265267}
266268
267269void Worker::Run () {
268- std::string name = " WorkerThread " ;
269- name += std::to_string (thread_id_. id );
270+ std::string trace_name = " [worker " + std::to_string (thread_id_. id ) + " ] " +
271+ (name_ == " " ? " " : " " + name_ );
270272 TRACE_EVENT_METADATA1 (
271- " __metadata" , " thread_name" , " name" ,
272- TRACE_STR_COPY (name.c_str ()));
273+ " __metadata" , " thread_name" , " name" , TRACE_STR_COPY (trace_name.c_str ()));
273274 CHECK_NOT_NULL (platform_);
274275
275276 Debug (this , " Creating isolate for worker with id %llu" , thread_id_.id );
@@ -470,6 +471,7 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
470471 }
471472
472473 std::string url;
474+ std::string name;
473475 std::shared_ptr<PerIsolateOptions> per_isolate_opts = nullptr ;
474476 std::shared_ptr<KVStore> env_vars = nullptr ;
475477
@@ -482,6 +484,12 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
482484 url.append (value.out (), value.length ());
483485 }
484486
487+ if (!args[5 ]->IsNullOrUndefined ()) {
488+ Utf8Value value (
489+ isolate, args[5 ]->ToString (env->context ()).FromMaybe (Local<String>()));
490+ name.append (value.out (), value.length ());
491+ }
492+
485493 if (args[1 ]->IsNull ()) {
486494 // Means worker.env = { ...process.env }.
487495 env_vars = env->env_vars ()->Clone (isolate);
@@ -592,6 +600,7 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
592600 Worker* worker = new Worker (env,
593601 args.This (),
594602 url,
603+ name,
595604 per_isolate_opts,
596605 std::move (exec_argv_out),
597606 env_vars,
0 commit comments