Skip to content

Commit be87651

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: #32344 PR-URL: #32562 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 0c70e8c commit be87651

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
@@ -592,16 +592,7 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
592592
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
593593
Mutex::ScopedLock lock(w->mutex_);
594594

595-
// The object now owns the created thread and should not be garbage collected
596-
// until that finishes.
597-
w->ClearWeak();
598-
599-
w->env()->add_sub_worker_context(w);
600595
w->stopped_ = false;
601-
w->thread_joined_ = false;
602-
603-
if (w->has_ref_)
604-
w->env()->add_refs(1);
605596

606597
uv_thread_options_t thread_options;
607598
thread_options.flags = UV_THREAD_HAS_STACK_SIZE;
@@ -628,21 +619,27 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
628619
// implicitly delete w
629620
});
630621
}, static_cast<void*>(w));
631-
if (ret != 0) {
622+
623+
if (ret == 0) {
624+
// The object now owns the created thread and should not be garbage
625+
// collected until that finishes.
626+
w->ClearWeak();
627+
w->thread_joined_ = false;
628+
629+
if (w->has_ref_)
630+
w->env()->add_refs(1);
631+
632+
w->env()->add_sub_worker_context(w);
633+
} else {
634+
w->stopped_ = true;
635+
632636
char err_buf[128];
633637
uv_err_name_r(ret, err_buf, sizeof(err_buf));
634-
w->custom_error_ = "ERR_WORKER_INIT_FAILED";
635-
w->custom_error_str_ = err_buf;
636-
w->loop_init_failed_ = true;
637-
w->thread_joined_ = true;
638-
w->stopped_ = true;
639-
w->env()->remove_sub_worker_context(w);
640638
{
641639
Isolate* isolate = w->env()->isolate();
642640
HandleScope handle_scope(isolate);
643641
THROW_ERR_WORKER_INIT_FAILED(isolate, err_buf);
644642
}
645-
w->MakeWeak();
646643
}
647644
}
648645

0 commit comments

Comments
 (0)