Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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
  • Loading branch information
addaleax committed Mar 30, 2020
commit 09d346994ec8dc1a5240e16ae7df1f9ddb2533c9
31 changes: 14 additions & 17 deletions src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -588,16 +588,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 +615,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
Copy Markdown
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
Copy Markdown
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