@@ -49,6 +49,7 @@ constexpr double kMB = 1024 * 1024;
4949Worker::Worker (Environment* env,
5050 Local<Object> wrap,
5151 const std::string& url,
52+ const std::string& name,
5253 std::shared_ptr<PerIsolateOptions> per_isolate_opts,
5354 std::vector<std::string>&& exec_argv,
5455 std::shared_ptr<KVStore> env_vars,
@@ -58,6 +59,7 @@ Worker::Worker(Environment* env,
5859 exec_argv_(exec_argv),
5960 platform_(env->isolate_data ()->platform()),
6061 thread_id_(AllocateEnvironmentThreadId()),
62+ name_(name),
6163 env_vars_(env_vars),
6264 snapshot_data_(snapshot_data) {
6365 Debug (this , " Creating new worker instance with thread id %llu" ,
@@ -82,8 +84,8 @@ Worker::Worker(Environment* env,
8284 Number::New (env->isolate (), static_cast <double >(thread_id_.id )))
8385 .Check ();
8486
85- inspector_parent_handle_ = GetInspectorParentHandle (
86- env, thread_id_, url.c_str ());
87+ inspector_parent_handle_ =
88+ GetInspectorParentHandle ( env, thread_id_, url. c_str (), name .c_str ());
8789
8890 argv_ = std::vector<std::string>{env->argv ()[0 ]};
8991 // Mark this Worker object as weak until we actually start the thread.
@@ -264,11 +266,10 @@ size_t Worker::NearHeapLimit(void* data, size_t current_heap_limit,
264266}
265267
266268void Worker::Run () {
267- std::string name = " WorkerThread " ;
268- name += std::to_string (thread_id_. id );
269+ std::string trace_name = " [worker " + std::to_string (thread_id_. id ) + " ] " +
270+ (name_ == " " ? " " : " " + name_ );
269271 TRACE_EVENT_METADATA1 (
270- " __metadata" , " thread_name" , " name" ,
271- TRACE_STR_COPY (name.c_str ()));
272+ " __metadata" , " thread_name" , " name" , TRACE_STR_COPY (trace_name.c_str ()));
272273 CHECK_NOT_NULL (platform_);
273274
274275 Debug (this , " Creating isolate for worker with id %llu" , thread_id_.id );
@@ -467,6 +468,7 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
467468 }
468469
469470 std::string url;
471+ std::string name;
470472 std::shared_ptr<PerIsolateOptions> per_isolate_opts = nullptr ;
471473 std::shared_ptr<KVStore> env_vars = nullptr ;
472474
@@ -479,6 +481,12 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
479481 url.append (value.out (), value.length ());
480482 }
481483
484+ if (!args[5 ]->IsNullOrUndefined ()) {
485+ Utf8Value value (
486+ isolate, args[5 ]->ToString (env->context ()).FromMaybe (Local<String>()));
487+ name.append (value.out (), value.length ());
488+ }
489+
482490 if (args[1 ]->IsNull ()) {
483491 // Means worker.env = { ...process.env }.
484492 env_vars = env->env_vars ()->Clone (isolate);
@@ -589,6 +597,7 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
589597 Worker* worker = new Worker (env,
590598 args.This (),
591599 url,
600+ name,
592601 per_isolate_opts,
593602 std::move (exec_argv_out),
594603 env_vars,
0 commit comments