From 9fcd394ec4a597d8689682f6fad93eb83f56ab16 Mon Sep 17 00:00:00 2001 From: Harshitha KP Date: Wed, 18 Mar 2020 10:26:00 -0400 Subject: [PATCH] src,worker: runtime error on pthread creation with large number of worker threads pthread fails with hard assertion. Instead of hard assertion throw a runtime error. Refs: https://github.com/nodejs/node/issues/32319 --- src/node_worker.cc | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) mode change 100644 => 100755 src/node_worker.cc diff --git a/src/node_worker.cc b/src/node_worker.cc old mode 100644 new mode 100755 index 6ff1a0afe9915b..d0244ae99c2329 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -623,7 +623,7 @@ void Worker::StartThread(const FunctionCallbackInfo& 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. @@ -644,7 +644,37 @@ void Worker::StartThread(const FunctionCallbackInfo& args) { w->JoinThread(); // implicitly delete w }); - }, static_cast(w)), 0); + }, static_cast(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 args[] = { + Integer::New(w->env()->isolate(), w->exit_code_), + w->custom_error_ != nullptr + ? OneByteString(w->env()->isolate(), w->custom_error_).As() + : Null(w->env()->isolate()).As(), + !w->custom_error_str_.empty() + ? OneByteString(w->env()->isolate(), w->custom_error_str_.c_str()) + .As() + : Null(w->env()->isolate()).As(), + }; + w->MakeCallback(w->env()->onexit_string(), arraysize(args), args); + } + w->MakeWeak(); + } } void Worker::StopThread(const FunctionCallbackInfo& args) {