diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index de3cb8f89c1ea2..d345552f909234 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,20 @@ 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); - }); + 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) { + 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; 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_); }