From 9fcd394ec4a597d8689682f6fad93eb83f56ab16 Mon Sep 17 00:00:00 2001 From: Harshitha KP Date: Wed, 18 Mar 2020 10:26:00 -0400 Subject: [PATCH 1/4] 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) { From c064d2d358ca8654849b2923792a3cd0e466113c Mon Sep 17 00:00:00 2001 From: Harshitha KP Date: Sat, 21 Mar 2020 02:34:26 -0400 Subject: [PATCH 2/4] fixup: convert emit to throw --- src/node_errors.h | 2 ++ src/node_worker.cc | 20 +++----------------- 2 files changed, 5 insertions(+), 17 deletions(-) mode change 100644 => 100755 src/node_errors.h diff --git a/src/node_errors.h b/src/node_errors.h old mode 100644 new mode 100755 index 2c421e80721484..48fcdf84867f5f --- a/src/node_errors.h +++ b/src/node_errors.h @@ -58,6 +58,7 @@ void OnFatalError(const char* location, const char* message); V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER, TypeError) \ V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, Error) \ V(ERR_VM_MODULE_CACHED_DATA_REJECTED, Error) \ + V(ERR_WORKER_INIT_FAILED, Error) \ V(ERR_PROTO_ACCESS, Error) #define V(code, type) \ @@ -107,6 +108,7 @@ void OnFatalError(const char* location, const char* message); V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER, \ "Cannot serialize externalized SharedArrayBuffer") \ V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, "Failed to set PSK identity hint") \ + V(ERR_WORKER_INIT_FAILED, "Worker initialization failure") \ V(ERR_PROTO_ACCESS, \ "Accessing Object.prototype.__proto__ has been " \ "disallowed with --disable-proto=throw") diff --git a/src/node_worker.cc b/src/node_worker.cc index d0244ae99c2329..0557247bb48756 100755 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -655,23 +655,9 @@ void Worker::StartThread(const FunctionCallbackInfo& args) { 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); + Isolate* isolate = w->env()->isolate(); + HandleScope handle_scope(isolate); + THROW_ERR_WORKER_INIT_FAILED(isolate, err_buf); } w->MakeWeak(); } From 2f5a48dbb7ba6155afcf7c06ce31cdcfdf0e72e5 Mon Sep 17 00:00:00 2001 From: Harshitha KP Date: Sun, 22 Mar 2020 23:59:28 -0400 Subject: [PATCH 3/4] fixup: address review comment --- src/node_worker.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_worker.cc b/src/node_worker.cc index 0557247bb48756..f8b2a915883d17 100755 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -659,7 +659,7 @@ void Worker::StartThread(const FunctionCallbackInfo& args) { HandleScope handle_scope(isolate); THROW_ERR_WORKER_INIT_FAILED(isolate, err_buf); } - w->MakeWeak(); + w->MakeWeak(); } } From 9a5b658e2db8019f356b4ed0b0117c7bb507b2ca Mon Sep 17 00:00:00 2001 From: Harshitha KP Date: Mon, 23 Mar 2020 05:55:02 -0400 Subject: [PATCH 4/4] fixup: address review comment --- src/node_errors.h | 0 src/node_worker.cc | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 src/node_errors.h mode change 100755 => 100644 src/node_worker.cc diff --git a/src/node_errors.h b/src/node_errors.h old mode 100755 new mode 100644 diff --git a/src/node_worker.cc b/src/node_worker.cc old mode 100755 new mode 100644