From 9788da962e4860b848bcfe1bcd15b76b0f7eedd8 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Wed, 6 Jun 2018 16:32:51 -0400 Subject: [PATCH] src: do not persist fs_poll handle in stat_watcher Instead of relying on garbage collection to close the handle, manage its state more explicitly. PR-URL: https://github.com/nodejs/node/pull/21093 Fixes: https://github.com/nodejs/node/issues/18190 Refs: https://github.com/nodejs/node/pull/18307 Reviewed-By: Jeremiah Senkpiel Reviewed-By: Anna Henningsen Reviewed-By: Ben Noordhuis Reviewed-By: Joyee Cheung Reviewed-By: Colin Ihrig --- src/node_stat_watcher.cc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/node_stat_watcher.cc b/src/node_stat_watcher.cc index 3f7da197b2a74d..0409cfbfb5fac1 100644 --- a/src/node_stat_watcher.cc +++ b/src/node_stat_watcher.cc @@ -77,19 +77,15 @@ void StatWatcher::Initialize(Environment* env, Local target) { StatWatcher::StatWatcher(Environment* env, Local wrap, bool use_bigint) : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_STATWATCHER), - watcher_(new uv_fs_poll_t), + watcher_(nullptr), use_bigint_(use_bigint) { MakeWeak(); - uv_fs_poll_init(env->event_loop(), watcher_); - watcher_->data = static_cast(this); } StatWatcher::~StatWatcher() { - if (IsActive()) { + if (IsActive()) Stop(); - } - env()->CloseHandle(watcher_, [](uv_fs_poll_t* handle) { delete handle; }); } @@ -123,7 +119,7 @@ void StatWatcher::New(const FunctionCallbackInfo& args) { } bool StatWatcher::IsActive() { - return uv_is_active(reinterpret_cast(watcher_)) != 0; + return watcher_ != nullptr; } void StatWatcher::IsActive(const v8::FunctionCallbackInfo& args) { @@ -156,6 +152,9 @@ void StatWatcher::Start(const FunctionCallbackInfo& args) { CHECK(args[2]->IsUint32()); const uint32_t interval = args[2].As()->Value(); + wrap->watcher_ = new uv_fs_poll_t(); + CHECK_EQ(0, uv_fs_poll_init(wrap->env()->event_loop(), wrap->watcher_)); + wrap->watcher_->data = static_cast(wrap); // Safe, uv_ref/uv_unref are idempotent. if (persistent) uv_ref(reinterpret_cast(wrap->watcher_)); @@ -187,7 +186,8 @@ void StatWatcher::Stop(const FunctionCallbackInfo& args) { void StatWatcher::Stop() { - uv_fs_poll_stop(watcher_); + env()->CloseHandle(watcher_, [](uv_fs_poll_t* handle) { delete handle; }); + watcher_ = nullptr; MakeWeak(); }