@@ -48,6 +48,7 @@ constexpr double kMB = 1024 * 1024;
4848Worker::Worker (Environment* env,
4949 Local<Object> wrap,
5050 const std::string& url,
51+ const std::string& name,
5152 std::shared_ptr<PerIsolateOptions> per_isolate_opts,
5253 std::vector<std::string>&& exec_argv,
5354 std::shared_ptr<KVStore> env_vars,
@@ -57,6 +58,7 @@ Worker::Worker(Environment* env,
5758 exec_argv_(exec_argv),
5859 platform_(env->isolate_data ()->platform()),
5960 thread_id_(AllocateEnvironmentThreadId()),
61+ name_(name),
6062 env_vars_(env_vars),
6163 snapshot_data_(snapshot_data) {
6264 Debug (this , " Creating new worker instance with thread id %llu" ,
@@ -81,8 +83,8 @@ Worker::Worker(Environment* env,
8183 Number::New (env->isolate (), static_cast <double >(thread_id_.id )))
8284 .Check ();
8385
84- inspector_parent_handle_ = GetInspectorParentHandle (
85- env, thread_id_, url.c_str ());
86+ inspector_parent_handle_ =
87+ GetInspectorParentHandle ( env, thread_id_, url. c_str (), name .c_str ());
8688
8789 argv_ = std::vector<std::string>{env->argv ()[0 ]};
8890 // Mark this Worker object as weak until we actually start the thread.
@@ -256,11 +258,10 @@ size_t Worker::NearHeapLimit(void* data, size_t current_heap_limit,
256258}
257259
258260void Worker::Run () {
259- std::string name = " WorkerThread " ;
260- name += std::to_string (thread_id_. id );
261+ std::string trace_name = " [worker " + std::to_string (thread_id_. id ) + " ] " +
262+ (name_ == " " ? " " : " " + name_ );
261263 TRACE_EVENT_METADATA1 (
262- " __metadata" , " thread_name" , " name" ,
263- TRACE_STR_COPY (name.c_str ()));
264+ " __metadata" , " thread_name" , " name" , TRACE_STR_COPY (trace_name.c_str ()));
264265 CHECK_NOT_NULL (platform_);
265266
266267 Debug (this , " Creating isolate for worker with id %llu" , thread_id_.id );
@@ -454,6 +455,7 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
454455 }
455456
456457 std::string url;
458+ std::string name;
457459 std::shared_ptr<PerIsolateOptions> per_isolate_opts = nullptr ;
458460 std::shared_ptr<KVStore> env_vars = nullptr ;
459461
@@ -466,6 +468,12 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
466468 url.append (value.out (), value.length ());
467469 }
468470
471+ if (!args[5 ]->IsNullOrUndefined ()) {
472+ Utf8Value value (
473+ isolate, args[5 ]->ToString (env->context ()).FromMaybe (Local<String>()));
474+ name.append (value.out (), value.length ());
475+ }
476+
469477 if (args[1 ]->IsNull ()) {
470478 // Means worker.env = { ...process.env }.
471479 env_vars = env->env_vars ()->Clone (isolate);
@@ -579,6 +587,7 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
579587 Worker* worker = new Worker (env,
580588 args.This (),
581589 url,
590+ name,
582591 per_isolate_opts,
583592 std::move (exec_argv_out),
584593 env_vars,
0 commit comments