Skip to content

Commit 0faaa7c

Browse files
addaleaxtargos
authored andcommitted
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 b6f7196 commit 0faaa7c

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/node_worker.cc

+14-17
Original file line numberDiff line numberDiff line change
@@ -609,16 +609,7 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
609609
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
610610
Mutex::ScopedLock lock(w->mutex_);
611611

612-
// The object now owns the created thread and should not be garbage collected
613-
// until that finishes.
614-
w->ClearWeak();
615-
616-
w->env()->add_sub_worker_context(w);
617612
w->stopped_ = false;
618-
w->thread_joined_ = false;
619-
620-
if (w->has_ref_)
621-
w->env()->add_refs(1);
622613

623614
uv_thread_options_t thread_options;
624615
thread_options.flags = UV_THREAD_HAS_STACK_SIZE;
@@ -645,21 +636,27 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
645636
// implicitly delete w
646637
});
647638
}, static_cast<void*>(w));
648-
if (ret != 0) {
639+
640+
if (ret == 0) {
641+
// The object now owns the created thread and should not be garbage
642+
// collected until that finishes.
643+
w->ClearWeak();
644+
w->thread_joined_ = false;
645+
646+
if (w->has_ref_)
647+
w->env()->add_refs(1);
648+
649+
w->env()->add_sub_worker_context(w);
650+
} else {
651+
w->stopped_ = true;
652+
649653
char err_buf[128];
650654
uv_err_name_r(ret, err_buf, sizeof(err_buf));
651-
w->custom_error_ = "ERR_WORKER_INIT_FAILED";
652-
w->custom_error_str_ = err_buf;
653-
w->loop_init_failed_ = true;
654-
w->thread_joined_ = true;
655-
w->stopped_ = true;
656-
w->env()->remove_sub_worker_context(w);
657655
{
658656
Isolate* isolate = w->env()->isolate();
659657
HandleScope handle_scope(isolate);
660658
THROW_ERR_WORKER_INIT_FAILED(isolate, err_buf);
661659
}
662-
w->MakeWeak();
663660
}
664661
}
665662

0 commit comments

Comments
 (0)