From c71067c585081981bcc965e79bd3b47d7165c1d4 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Tue, 8 Aug 2017 20:23:16 +0800 Subject: [PATCH] dns: make `library_inited_` state global Once `ares_library_init(ARES_LIB_INIT_ALL)` succeeded, it shouldn't be initialized again. So make the flag `library_inited_` global. --- src/cares_wrap.cc | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 53a005444cc498..d0a9f36e34e154 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -141,6 +141,7 @@ class ChannelWrap : public AsyncWrap { inline uv_timer_t* timer_handle() { return timer_handle_; } inline ares_channel cares_channel() { return channel_; } inline bool query_last_ok() const { return query_last_ok_; } + static inline bool library_inited() { return library_inited_; } inline void set_query_last_ok(bool ok) { query_last_ok_ = ok; } inline bool is_servers_default() const { return is_servers_default_; } inline void set_is_servers_default(bool is_default) { @@ -157,18 +158,19 @@ class ChannelWrap : public AsyncWrap { ares_channel channel_; bool query_last_ok_; bool is_servers_default_; - bool library_inited_; + static bool library_inited_; node_ares_task_list task_list_; }; +bool ChannelWrap::library_inited_ = false; + ChannelWrap::ChannelWrap(Environment* env, Local object) : AsyncWrap(env, object, PROVIDER_DNSCHANNEL), timer_handle_(nullptr), channel_(nullptr), query_last_ok_(true), - is_servers_default_(true), - library_inited_(false) { + is_servers_default_(true) { RB_INIT(&task_list_); MakeWeak(this); @@ -493,6 +495,7 @@ void ChannelWrap::Setup() { if (r != ARES_SUCCESS) return env()->ThrowError(ToErrorCodeString(r)); } + library_inited_ = true; /* We do the call to ares_init_option for caller. */ r = ares_init_options(&channel_, @@ -500,12 +503,9 @@ void ChannelWrap::Setup() { ARES_OPT_FLAGS | ARES_OPT_SOCK_STATE_CB); if (r != ARES_SUCCESS) { - ares_library_cleanup(); return env()->ThrowError(ToErrorCodeString(r)); } - library_inited_ = true; - /* Initialize the timeout timer. The timer won't be started until the */ /* first socket is opened. */ CleanupTimer(); @@ -516,9 +516,6 @@ void ChannelWrap::Setup() { ChannelWrap::~ChannelWrap() { - if (library_inited_) - ares_library_cleanup(); - ares_destroy(channel_); CleanupTimer(); }