Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: schedule unref immediate for uv_close in destructors #18307

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ class ChannelWrap : public AsyncWrap {

void Setup();
void EnsureServers();
void CleanupTimer();

void ModifyActivityQueryCount(int count);

Expand Down Expand Up @@ -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<uv_timer_t*>(handle);
};
uv_close(reinterpret_cast<uv_handle_t*>(timer_handle_), close_cb);
}
timer_handle_ = new uv_timer_t();
timer_handle_->data = static_cast<void*>(this);
uv_timer_init(env()->event_loop(), timer_handle_);
Expand All @@ -517,20 +521,20 @@ ChannelWrap::~ChannelWrap() {
}

ares_destroy(channel_);
CleanupTimer();
}


void ChannelWrap::CleanupTimer() {
if (timer_handle_ == nullptr) return;

uv_close(reinterpret_cast<uv_handle_t*>(timer_handle_),
[](uv_handle_t* handle) {
delete reinterpret_cast<uv_timer_t*>(handle);
});
if (timer_handle_ == nullptr)
return;
uv_timer_stop(timer_handle_);
auto close_cb = [](Environment* env, void* data) {
uv_close(reinterpret_cast<uv_handle_t*>(data), [](uv_handle_t* handle) {
delete reinterpret_cast<uv_timer_t*>(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;
Expand Down
12 changes: 6 additions & 6 deletions src/node_stat_watcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ void StatWatcher::Initialize(Environment* env, Local<Object> target) {
}


static void Delete(uv_handle_t* handle) {
delete reinterpret_cast<uv_fs_poll_t*>(handle);
}


StatWatcher::StatWatcher(Environment* env, Local<Object> wrap)
: AsyncWrap(env, wrap, AsyncWrap::PROVIDER_STATWATCHER),
watcher_(new uv_fs_poll_t) {
Expand All @@ -74,7 +69,12 @@ StatWatcher::StatWatcher(Environment* env, Local<Object> wrap)

StatWatcher::~StatWatcher() {
Stop();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I look at it... calling Stop() here is probably a minor (if harmless) bug as it in turn calls MakeWeak() when the destructor is almost certainly invoked from a weak callback.

You didn't introduce it so it's fine to leave it be for now but I figured I'd point it out.

uv_close(reinterpret_cast<uv_handle_t*>(watcher_), Delete);
auto close_cb = [](Environment* env, void* data) {
uv_close(reinterpret_cast<uv_handle_t*>(data), [](uv_handle_t* handle) {
delete reinterpret_cast<uv_fs_poll_t*>(handle);
});
};
env()->SetUnrefImmediate(close_cb, watcher_);
}


Expand Down