From e324fc4cfb2c235a9bd9e0b80e74a114900ea0b0 Mon Sep 17 00:00:00 2001 From: "trop[bot]" Date: Tue, 11 Dec 2018 16:11:23 +0900 Subject: [PATCH] fix: allow 2 threads for CreateIoCompletionPort on single-core to prevent busy looping (backport: 4-0-x) (#16012) * allow 2 threads for CreateIoCompletionPort on single-core * use base::SysInfo::NumberOfProcessors instead of env var * CHECK that uv_loop_ has not been used before replacing its iocp --- atom/common/node_bindings_win.cc | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/atom/common/node_bindings_win.cc b/atom/common/node_bindings_win.cc index e41fb585f5711..b0ab715afc1b8 100644 --- a/atom/common/node_bindings_win.cc +++ b/atom/common/node_bindings_win.cc @@ -7,11 +7,25 @@ #include #include "base/logging.h" +#include "base/sys_info.h" namespace atom { NodeBindingsWin::NodeBindingsWin(BrowserEnvironment browser_env) - : NodeBindings(browser_env) {} + : NodeBindings(browser_env) { + // on single-core the io comp port NumberOfConcurrentThreads needs to be 2 + // to avoid cpu pegging likely caused by a busy loop in PollEvents + if (base::SysInfo::NumberOfProcessors() == 1) { + // the expectation is the uv_loop_ has just been initialized + // which makes iocp replacement safe + CHECK_EQ(0u, uv_loop_->active_handles); + CHECK_EQ(0u, uv_loop_->active_reqs.count); + + if (uv_loop_->iocp && uv_loop_->iocp != INVALID_HANDLE_VALUE) + CloseHandle(uv_loop_->iocp); + uv_loop_->iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 2); + } +} NodeBindingsWin::~NodeBindingsWin() {}