@@ -264,22 +264,6 @@ void Worker::Run() {
264264 Debug (this , " Created Environment for worker with id %llu" , thread_id_);
265265 if (is_stopped ()) return ;
266266 {
267- HandleScope handle_scope (isolate_);
268- Mutex::ScopedLock lock (mutex_);
269- // Set up the message channel for receiving messages in the child.
270- child_port_ = MessagePort::New (env_.get (),
271- env_->context (),
272- std::move (child_port_data_));
273- // MessagePort::New() may return nullptr if execution is terminated
274- // within it.
275- if (child_port_ != nullptr )
276- env_->set_message_port (child_port_->object (isolate_));
277-
278- Debug (this , " Created message port for worker %llu" , thread_id_);
279- }
280-
281- if (is_stopped ()) return ;
282- {
283267#if NODE_USE_V8_PLATFORM && HAVE_INSPECTOR
284268 StartWorkerInspector (env_.get (),
285269 std::move (inspector_parent_handle_),
@@ -291,6 +275,9 @@ void Worker::Run() {
291275 AsyncCallbackScope callback_scope (env_.get ());
292276 env_->async_hooks ()->push_async_ids (1 , 0 );
293277 if (!RunBootstrapping (env_.get ()).IsEmpty ()) {
278+ CreateEnvMessagePort (env_.get ());
279+ if (is_stopped ()) return ;
280+ Debug (this , " Created message port for worker %llu" , thread_id_);
294281 USE (StartExecution (env_.get (), " internal/main/worker_thread" ));
295282 }
296283
@@ -343,6 +330,19 @@ void Worker::Run() {
343330 Debug (this , " Worker %llu thread stops" , thread_id_);
344331}
345332
333+ void Worker::CreateEnvMessagePort (Environment* env) {
334+ HandleScope handle_scope (isolate_);
335+ Mutex::ScopedLock lock (mutex_);
336+ // Set up the message channel for receiving messages in the child.
337+ child_port_ = MessagePort::New (env,
338+ env->context (),
339+ std::move (child_port_data_));
340+ // MessagePort::New() may return nullptr if execution is terminated
341+ // within it.
342+ if (child_port_ != nullptr )
343+ env->set_message_port (child_port_->object (isolate_));
344+ }
345+
346346void Worker::JoinThread () {
347347 if (thread_joined_)
348348 return ;
0 commit comments