From c939d4e943986ee709a88a76effe6bc5a3a6221f Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Mon, 22 Jan 2018 21:13:39 -0500 Subject: [PATCH 1/3] cares_wrap: use unref immediate to uv_close in destructor --- src/cares_wrap.cc | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index de3cb8f89c1ea2..1525d596263353 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -149,7 +149,6 @@ class ChannelWrap : public AsyncWrap { void Setup(); void EnsureServers(); - void CleanupTimer(); void ModifyActivityQueryCount(int count); @@ -503,7 +502,12 @@ void ChannelWrap::Setup() { /* Initialize the timeout timer. The timer won't be started until the */ /* first socket is opened. */ - CleanupTimer(); + if (timer_handle_ != nullptr) { + auto close_cb = [](uv_handle_t* handle) { + delete reinterpret_cast(handle); + }; + uv_close(reinterpret_cast(timer_handle_), close_cb); + } timer_handle_ = new uv_timer_t(); timer_handle_->data = static_cast(this); uv_timer_init(env()->event_loop(), timer_handle_); @@ -517,20 +521,17 @@ ChannelWrap::~ChannelWrap() { } ares_destroy(channel_); - CleanupTimer(); -} - - -void ChannelWrap::CleanupTimer() { - if (timer_handle_ == nullptr) return; - - uv_close(reinterpret_cast(timer_handle_), - [](uv_handle_t* handle) { - delete reinterpret_cast(handle); - }); + uv_timer_stop(timer_handle_); + auto close_cb = [](Environment* env, void* data) { + uv_close(reinterpret_cast(data), [](uv_handle_t* handle) { + delete reinterpret_cast(handle); + }); + }; + env()->SetUnrefImmediate(close_cb, timer_handle_); timer_handle_ = nullptr; } + void ChannelWrap::ModifyActivityQueryCount(int count) { active_query_count_ += count; if (active_query_count_ < 0) active_query_count_ = 0; From 9b5e6710372485bf78485679b62f187b8fc48848 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Mon, 22 Jan 2018 21:14:06 -0500 Subject: [PATCH 2/3] fs: use unref immediate to uv_close in destructor --- src/node_stat_watcher.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/node_stat_watcher.cc b/src/node_stat_watcher.cc index 9aa0c950591d16..cdf3257d83e2c6 100644 --- a/src/node_stat_watcher.cc +++ b/src/node_stat_watcher.cc @@ -57,11 +57,6 @@ void StatWatcher::Initialize(Environment* env, Local target) { } -static void Delete(uv_handle_t* handle) { - delete reinterpret_cast(handle); -} - - StatWatcher::StatWatcher(Environment* env, Local wrap) : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_STATWATCHER), watcher_(new uv_fs_poll_t) { @@ -74,7 +69,12 @@ StatWatcher::StatWatcher(Environment* env, Local wrap) StatWatcher::~StatWatcher() { Stop(); - uv_close(reinterpret_cast(watcher_), Delete); + auto close_cb = [](Environment* env, void* data) { + uv_close(reinterpret_cast(data), [](uv_handle_t* handle) { + delete reinterpret_cast(handle); + }); + }; + env()->SetUnrefImmediate(close_cb, watcher_); } From fae83aec26ed0936a8ad03beacf7d63399a601c2 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Wed, 24 Jan 2018 09:24:47 -0500 Subject: [PATCH 3/3] fixup: add nullptr check --- src/cares_wrap.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 1525d596263353..d345552f909234 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -521,6 +521,9 @@ ChannelWrap::~ChannelWrap() { } ares_destroy(channel_); + + if (timer_handle_ == nullptr) + return; uv_timer_stop(timer_handle_); auto close_cb = [](Environment* env, void* data) { uv_close(reinterpret_cast(data), [](uv_handle_t* handle) {