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: properly manage timer in cares ChannelWrap #14634

Closed
wants to merge 5 commits into from
Closed
Changes from 3 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
39 changes: 24 additions & 15 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ class ChannelWrap : public AsyncWrap {

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

inline uv_timer_t* timer_handle() { return &timer_handle_; }
inline uv_timer_t* timer_handle() { return timer_handle_; }
inline ares_channel cares_channel() { return channel_; }
inline bool query_last_ok() const { return query_last_ok_; }
inline void set_query_last_ok(bool ok) { query_last_ok_ = ok; }
Expand All @@ -152,7 +153,7 @@ class ChannelWrap : public AsyncWrap {
static void AresTimeout(uv_timer_t* handle);

private:
uv_timer_t timer_handle_;
uv_timer_t* timer_handle_;
ares_channel channel_;
bool query_last_ok_;
bool is_servers_default_;
Expand All @@ -163,6 +164,7 @@ class ChannelWrap : public AsyncWrap {
ChannelWrap::ChannelWrap(Environment* env,
Local<Object> object)
: AsyncWrap(env, object, PROVIDER_DNSCHANNEL),
timer_handle_(nullptr),
channel_(nullptr),
query_last_ok_(true),
is_servers_default_(true),
Expand Down Expand Up @@ -236,7 +238,8 @@ RB_GENERATE_STATIC(node_ares_task_list, node_ares_task, node, cmp_ares_tasks)
/* This is called once per second by loop->timer. It is used to constantly */
/* call back into c-ares for possibly processing timeouts. */
void ChannelWrap::AresTimeout(uv_timer_t* handle) {
ChannelWrap* channel = ContainerOf(&ChannelWrap::timer_handle_, handle);
ChannelWrap* channel = static_cast<ChannelWrap*>(handle->data);
CHECK_EQ(channel->timer_handle(), handle);
CHECK_EQ(false, RB_EMPTY(channel->task_list()));
ares_process_fd(channel->cares_channel(), ARES_SOCKET_BAD, ARES_SOCKET_BAD);
}
Expand Down Expand Up @@ -505,25 +508,31 @@ void ChannelWrap::Setup() {

/* Initialize the timeout timer. The timer won't be started until the */
/* first socket is opened. */
uv_timer_init(env()->event_loop(), &timer_handle_);
env()->RegisterHandleCleanup(
reinterpret_cast<uv_handle_t*>(&timer_handle_),
[](Environment* env, uv_handle_t* handle, void* arg) {
uv_close(handle, [](uv_handle_t* handle) {
ChannelWrap* channel = ContainerOf(
&ChannelWrap::timer_handle_,
reinterpret_cast<uv_timer_t*>(handle));
channel->env()->FinishHandleCleanup(handle);
});
},
nullptr);
CleanupTimer();
timer_handle_ = new uv_timer_t();
timer_handle_->data = static_cast<void*>(this);
uv_timer_init(env()->event_loop(), timer_handle_);
}


ChannelWrap::~ChannelWrap() {
if (library_inited_)
ares_library_cleanup();

ares_destroy(channel_);
CleanupTimer();
}

Copy link
Member

Choose a reason for hiding this comment

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

Double newline here, please.


void ChannelWrap::CleanupTimer() {
if (!timer_handle_) return;
Copy link
Member

Choose a reason for hiding this comment

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

if (timer_handle_ == nullptr) return;


uv_close(reinterpret_cast<uv_handle_t*>(timer_handle_),
[](uv_handle_t* handle) {
handle->data = nullptr;
Copy link
Member

Choose a reason for hiding this comment

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

Dead store; the memory is deleted immediately afterwards.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, this was requested by @indutny and added by @refack. If they had any particular reason for that, please speak up, but I’ve removed it again for now.

delete reinterpret_cast<uv_timer_t*>(handle);
Copy link
Member

Choose a reason for hiding this comment

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

Not absolutely necessary, but doing handle->data = NULL prior to this may help in the future.

});
timer_handle_ = nullptr;
}


Expand Down