Skip to content

Commit 9f6ed72

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 4b96fc5 commit 9f6ed72

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

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

625616
uv_thread_options_t thread_options;
626617
thread_options.flags = UV_THREAD_HAS_STACK_SIZE;
@@ -647,21 +638,27 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
647638
// implicitly delete w
648639
});
649640
}, static_cast<void*>(w));
650-
if (ret != 0) {
641+
642+
if (ret == 0) {
643+
// The object now owns the created thread and should not be garbage
644+
// collected until that finishes.
645+
w->ClearWeak();
646+
w->thread_joined_ = false;
647+
648+
if (w->has_ref_)
649+
w->env()->add_refs(1);
650+
651+
w->env()->add_sub_worker_context(w);
652+
} else {
653+
w->stopped_ = true;
654+
651655
char err_buf[128];
652656
uv_err_name_r(ret, err_buf, sizeof(err_buf));
653-
w->custom_error_ = "ERR_WORKER_INIT_FAILED";
654-
w->custom_error_str_ = err_buf;
655-
w->loop_init_failed_ = true;
656-
w->thread_joined_ = true;
657-
w->stopped_ = true;
658-
w->env()->remove_sub_worker_context(w);
659657
{
660658
Isolate* isolate = w->env()->isolate();
661659
HandleScope handle_scope(isolate);
662660
THROW_ERR_WORKER_INIT_FAILED(isolate, err_buf);
663661
}
664-
w->MakeWeak();
665662
}
666663
}
667664

0 commit comments

Comments
 (0)