Skip to content

Commit 09d3469

Browse files
committed
src: clean up worker thread creation code
Instead of setting and then in the case of error un-setting properties, only set them when no error occurs. Refs: nodejs#32344
1 parent b6459ec commit 09d3469

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/node_worker.cc

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -588,16 +588,7 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
588588
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
589589
Mutex::ScopedLock lock(w->mutex_);
590590

591-
// The object now owns the created thread and should not be garbage collected
592-
// until that finishes.
593-
w->ClearWeak();
594-
595-
w->env()->add_sub_worker_context(w);
596591
w->stopped_ = false;
597-
w->thread_joined_ = false;
598-
599-
if (w->has_ref_)
600-
w->env()->add_refs(1);
601592

602593
uv_thread_options_t thread_options;
603594
thread_options.flags = UV_THREAD_HAS_STACK_SIZE;
@@ -624,21 +615,27 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
624615
// implicitly delete w
625616
});
626617
}, static_cast<void*>(w));
627-
if (ret != 0) {
618+
619+
if (ret == 0) {
620+
// The object now owns the created thread and should not be garbage
621+
// collected until that finishes.
622+
w->ClearWeak();
623+
w->thread_joined_ = false;
624+
625+
if (w->has_ref_)
626+
w->env()->add_refs(1);
627+
628+
w->env()->add_sub_worker_context(w);
629+
} else {
630+
w->stopped_ = true;
631+
628632
char err_buf[128];
629633
uv_err_name_r(ret, err_buf, sizeof(err_buf));
630-
w->custom_error_ = "ERR_WORKER_INIT_FAILED";
631-
w->custom_error_str_ = err_buf;
632-
w->loop_init_failed_ = true;
633-
w->thread_joined_ = true;
634-
w->stopped_ = true;
635-
w->env()->remove_sub_worker_context(w);
636634
{
637635
Isolate* isolate = w->env()->isolate();
638636
HandleScope handle_scope(isolate);
639637
THROW_ERR_WORKER_INIT_FAILED(isolate, err_buf);
640638
}
641-
w->MakeWeak();
642639
}
643640
}
644641

0 commit comments

Comments
 (0)