Skip to content

Commit

Permalink
src,worker: runtime error on pthread creation
Browse files Browse the repository at this point in the history
with large number of worker threads pthread
fails with hard assertion.
Instead of hard assertion throw a runtime error.

Refs: nodejs#32319
  • Loading branch information
HarshithaKP committed Mar 21, 2020
1 parent d129e0c commit 9fcd394
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/node_worker.cc
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
uv_thread_options_t thread_options;
thread_options.flags = UV_THREAD_HAS_STACK_SIZE;
thread_options.stack_size = kStackSize;
CHECK_EQ(uv_thread_create_ex(&w->tid_, &thread_options, [](void* arg) {
int ret = uv_thread_create_ex(&w->tid_, &thread_options, [](void* arg) {
// XXX: This could become a std::unique_ptr, but that makes at least
// gcc 6.3 detect undefined behaviour when there shouldn't be any.
// gcc 7+ handles this well.
Expand All @@ -644,7 +644,37 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
w->JoinThread();
// implicitly delete w
});
}, static_cast<void*>(w)), 0);
}, static_cast<void*>(w));
if (ret != 0) {
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);
{
HandleScope handle_scope(w->env()->isolate());
Context::Scope context_scope(w->env()->context());
// Reset the parent port as we're closing it now anyway.
w->object()->Set(w->env()->context(),
w->env()->message_port_string(),
Undefined(w->env()->isolate())).Check();
Local<Value> args[] = {
Integer::New(w->env()->isolate(), w->exit_code_),
w->custom_error_ != nullptr
? OneByteString(w->env()->isolate(), w->custom_error_).As<Value>()
: Null(w->env()->isolate()).As<Value>(),
!w->custom_error_str_.empty()
? OneByteString(w->env()->isolate(), w->custom_error_str_.c_str())
.As<Value>()
: Null(w->env()->isolate()).As<Value>(),
};
w->MakeCallback(w->env()->onexit_string(), arraysize(args), args);
}
w->MakeWeak();
}
}

void Worker::StopThread(const FunctionCallbackInfo<Value>& args) {
Expand Down

0 comments on commit 9fcd394

Please sign in to comment.