@@ -269,22 +269,6 @@ void Worker::Run() {
269269 Debug (this , " Created Environment for worker with id %llu" , thread_id_);
270270 if (is_stopped ()) return ;
271271 {
272- HandleScope handle_scope (isolate_);
273- Mutex::ScopedLock lock (mutex_);
274- // Set up the message channel for receiving messages in the child.
275- child_port_ = MessagePort::New (env_.get (),
276- env_->context (),
277- std::move (child_port_data_));
278- // MessagePort::New() may return nullptr if execution is terminated
279- // within it.
280- if (child_port_ != nullptr )
281- env_->set_message_port (child_port_->object (isolate_));
282-
283- Debug (this , " Created message port for worker %llu" , thread_id_);
284- }
285-
286- if (is_stopped ()) return ;
287- {
288272#if NODE_USE_V8_PLATFORM && HAVE_INSPECTOR
289273 StartWorkerInspector (env_.get (),
290274 std::move (inspector_parent_handle_),
@@ -296,6 +280,9 @@ void Worker::Run() {
296280 Environment::AsyncCallbackScope callback_scope (env_.get ());
297281 env_->async_hooks ()->push_async_ids (1 , 0 );
298282 if (!RunBootstrapping (env_.get ()).IsEmpty ()) {
283+ CreateEnvMessagePort (env_.get ());
284+ if (is_stopped ()) return ;
285+ Debug (this , " Created message port for worker %llu" , thread_id_);
299286 USE (StartExecution (env_.get (), " internal/main/worker_thread" ));
300287 }
301288
@@ -348,6 +335,19 @@ void Worker::Run() {
348335 Debug (this , " Worker %llu thread stops" , thread_id_);
349336}
350337
338+ void Worker::CreateEnvMessagePort (Environment* env) {
339+ HandleScope handle_scope (isolate_);
340+ Mutex::ScopedLock lock (mutex_);
341+ // Set up the message channel for receiving messages in the child.
342+ child_port_ = MessagePort::New (env,
343+ env->context (),
344+ std::move (child_port_data_));
345+ // MessagePort::New() may return nullptr if execution is terminated
346+ // within it.
347+ if (child_port_ != nullptr )
348+ env->set_message_port (child_port_->object (isolate_));
349+ }
350+
351351void Worker::JoinThread () {
352352 if (thread_joined_)
353353 return ;
0 commit comments