Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: minor worker code cleanups #32562

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 21 additions & 21 deletions src/node_worker.cc
Expand Up @@ -135,10 +135,10 @@ class WorkerThreadData {
uv_err_name_r(ret, err_buf, sizeof(err_buf));
w->custom_error_ = "ERR_WORKER_INIT_FAILED";
w->custom_error_str_ = err_buf;
w->loop_init_failed_ = true;
w->stopped_ = true;
return;
}
loop_init_failed_ = false;

std::shared_ptr<ArrayBufferAllocator> allocator =
ArrayBufferAllocator::Create();
Expand Down Expand Up @@ -194,6 +194,7 @@ class WorkerThreadData {
}

if (isolate != nullptr) {
CHECK(!loop_init_failed_);
bool platform_finished = false;

isolate_data_.reset();
Expand All @@ -212,18 +213,20 @@ class WorkerThreadData {

// Wait until the platform has cleaned up all relevant resources.
while (!platform_finished) {
CHECK(!w_->loop_init_failed_);
uv_run(&loop_, UV_RUN_ONCE);
}
}
if (!w_->loop_init_failed_) {
if (!loop_init_failed_) {
CheckedUvLoopClose(&loop_);
}
}

bool loop_is_usable() const { return !loop_init_failed_; }

private:
Worker* const w_;
uv_loop_t loop_;
bool loop_init_failed_ = true;
DeleteFnPtr<IsolateData, FreeIsolateData> isolate_data_;

friend class Worker;
Expand Down Expand Up @@ -253,7 +256,7 @@ void Worker::Run() {

WorkerThreadData data(this);
if (isolate_ == nullptr) return;
CHECK(!data.w_->loop_init_failed_);
CHECK(data.loop_is_usable());

Debug(this, "Starting worker with id %llu", thread_id_.id);
{
Expand Down Expand Up @@ -588,16 +591,7 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
Mutex::ScopedLock lock(w->mutex_);

// The object now owns the created thread and should not be garbage collected
// until that finishes.
w->ClearWeak();

w->env()->add_sub_worker_context(w);
w->stopped_ = false;
w->thread_joined_ = false;

if (w->has_ref_)
w->env()->add_refs(1);

uv_thread_options_t thread_options;
thread_options.flags = UV_THREAD_HAS_STACK_SIZE;
Expand All @@ -624,21 +618,27 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
// implicitly delete w
});
}, static_cast<void*>(w));
if (ret != 0) {

if (ret == 0) {
// The object now owns the created thread and should not be garbage
// collected until that finishes.
w->ClearWeak();
w->thread_joined_ = false;

if (w->has_ref_)
w->env()->add_refs(1);

w->env()->add_sub_worker_context(w);
} else {
w->stopped_ = true;

char err_buf[128];
uv_err_name_r(ret, err_buf, sizeof(err_buf));
w->custom_error_ = "ERR_WORKER_INIT_FAILED";
w->custom_error_str_ = err_buf;
w->loop_init_failed_ = true;
w->thread_joined_ = true;
w->stopped_ = true;
w->env()->remove_sub_worker_context(w);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why these assignments are removed ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HarshithaKP Because they either had no effect or were just un-setting variables that were set above.

{
Isolate* isolate = w->env()->isolate();
HandleScope handle_scope(isolate);
THROW_ERR_WORKER_INIT_FAILED(isolate, err_buf);
}
w->MakeWeak();
}
}

Expand Down
1 change: 0 additions & 1 deletion src/node_worker.h
Expand Up @@ -83,7 +83,6 @@ class Worker : public AsyncWrap {
bool thread_joined_ = true;
const char* custom_error_ = nullptr;
std::string custom_error_str_;
bool loop_init_failed_ = false;
int exit_code_ = 0;
ThreadId thread_id_;
uintptr_t stack_base_ = 0;
Expand Down