From ade4ec6f9aebb3f5b2963b885ab9e62c954ddc73 Mon Sep 17 00:00:00 2001 From: Harshitha KP Date: Wed, 18 Mar 2020 10:26:00 -0400 Subject: [PATCH] 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. PR-URL: https://github.com/nodejs/node/pull/32344 Fixes: https://github.com/nodejs/node/issues/32319 Reviewed-By: Ben Noordhuis Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- src/node_errors.h | 2 ++ src/node_worker.cc | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/node_errors.h b/src/node_errors.h index 2c421e80721484..48fcdf84867f5f 100644 --- 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 acee02d218cdda..5b0fd307fcd05b 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -625,7 +625,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. @@ -646,7 +646,23 @@ 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); + { + Isolate* isolate = w->env()->isolate(); + HandleScope handle_scope(isolate); + THROW_ERR_WORKER_INIT_FAILED(isolate, err_buf); + } + w->MakeWeak(); + } } void Worker::StopThread(const FunctionCallbackInfo& args) {