Skip to content

Commit 4f01168

Browse files
apapirovskitargos
authored andcommitted
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: #21093 Fixes: #18190 Refs: #18307 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 685b9b2 commit 4f01168

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/node_stat_watcher.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,15 @@ void StatWatcher::Initialize(Environment* env, Local<Object> target) {
7777

7878
StatWatcher::StatWatcher(Environment* env, Local<Object> wrap, bool use_bigint)
7979
: AsyncWrap(env, wrap, AsyncWrap::PROVIDER_STATWATCHER),
80-
watcher_(new uv_fs_poll_t),
80+
watcher_(nullptr),
8181
use_bigint_(use_bigint) {
8282
MakeWeak();
83-
uv_fs_poll_init(env->event_loop(), watcher_);
84-
watcher_->data = static_cast<void*>(this);
8583
}
8684

8785

8886
StatWatcher::~StatWatcher() {
89-
if (IsActive()) {
87+
if (IsActive())
9088
Stop();
91-
}
92-
env()->CloseHandle(watcher_, [](uv_fs_poll_t* handle) { delete handle; });
9389
}
9490

9591

@@ -123,7 +119,7 @@ void StatWatcher::New(const FunctionCallbackInfo<Value>& args) {
123119
}
124120

125121
bool StatWatcher::IsActive() {
126-
return uv_is_active(reinterpret_cast<uv_handle_t*>(watcher_)) != 0;
122+
return watcher_ != nullptr;
127123
}
128124

129125
void StatWatcher::IsActive(const v8::FunctionCallbackInfo<v8::Value>& args) {
@@ -156,6 +152,9 @@ void StatWatcher::Start(const FunctionCallbackInfo<Value>& args) {
156152
CHECK(args[2]->IsUint32());
157153
const uint32_t interval = args[2].As<Uint32>()->Value();
158154

155+
wrap->watcher_ = new uv_fs_poll_t();
156+
CHECK_EQ(0, uv_fs_poll_init(wrap->env()->event_loop(), wrap->watcher_));
157+
wrap->watcher_->data = static_cast<void*>(wrap);
159158
// Safe, uv_ref/uv_unref are idempotent.
160159
if (persistent)
161160
uv_ref(reinterpret_cast<uv_handle_t*>(wrap->watcher_));
@@ -187,7 +186,8 @@ void StatWatcher::Stop(const FunctionCallbackInfo<Value>& args) {
187186

188187

189188
void StatWatcher::Stop() {
190-
uv_fs_poll_stop(watcher_);
189+
env()->CloseHandle(watcher_, [](uv_fs_poll_t* handle) { delete handle; });
190+
watcher_ = nullptr;
191191
MakeWeak();
192192
}
193193

0 commit comments

Comments
 (0)