Skip to content

Commit 685b9b2

Browse files
apapirovskitargos
authored andcommitted
src: do not persist timer handle in cares_wrap
Instead of relying on garbage collection to close the timer 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 5e46c16 commit 685b9b2

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

src/cares_wrap.cc

+23-23
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ class ChannelWrap : public AsyncWrap {
151151

152152
void Setup();
153153
void EnsureServers();
154-
void CleanupTimer();
154+
void StartTimer();
155+
void CloseTimer();
155156

156157
void ModifyActivityQueryCount(int count);
157158

@@ -313,13 +314,7 @@ void ares_sockstate_cb(void* data,
313314
if (read || write) {
314315
if (!task) {
315316
/* New socket */
316-
317-
/* If this is the first socket then start the timer. */
318-
uv_timer_t* timer_handle = channel->timer_handle();
319-
if (!uv_is_active(reinterpret_cast<uv_handle_t*>(timer_handle))) {
320-
CHECK(channel->task_list()->empty());
321-
uv_timer_start(timer_handle, ChannelWrap::AresTimeout, 1000, 1000);
322-
}
317+
channel->StartTimer();
323318

324319
task = ares_task_create(channel, sock);
325320
if (task == nullptr) {
@@ -349,7 +344,7 @@ void ares_sockstate_cb(void* data,
349344
channel->env()->CloseHandle(&task->poll_watcher, ares_poll_close_cb);
350345

351346
if (channel->task_list()->empty()) {
352-
uv_timer_stop(channel->timer_handle());
347+
channel->CloseTimer();
353348
}
354349
}
355350
}
@@ -490,15 +485,26 @@ void ChannelWrap::Setup() {
490485
}
491486

492487
library_inited_ = true;
488+
}
493489

494-
/* Initialize the timeout timer. The timer won't be started until the */
495-
/* first socket is opened. */
496-
CleanupTimer();
497-
timer_handle_ = new uv_timer_t();
498-
timer_handle_->data = static_cast<void*>(this);
499-
uv_timer_init(env()->event_loop(), timer_handle_);
490+
void ChannelWrap::StartTimer() {
491+
if (timer_handle_ == nullptr) {
492+
timer_handle_ = new uv_timer_t();
493+
timer_handle_->data = static_cast<void*>(this);
494+
uv_timer_init(env()->event_loop(), timer_handle_);
495+
} else if (uv_is_active(reinterpret_cast<uv_handle_t*>(timer_handle_))) {
496+
return;
497+
}
498+
uv_timer_start(timer_handle_, AresTimeout, 1000, 1000);
500499
}
501500

501+
void ChannelWrap::CloseTimer() {
502+
if (timer_handle_ == nullptr)
503+
return;
504+
505+
env()->CloseHandle(timer_handle_, [](uv_timer_t* handle) { delete handle; });
506+
timer_handle_ = nullptr;
507+
}
502508

503509
ChannelWrap::~ChannelWrap() {
504510
if (library_inited_) {
@@ -508,17 +514,10 @@ ChannelWrap::~ChannelWrap() {
508514
}
509515

510516
ares_destroy(channel_);
511-
CleanupTimer();
517+
CloseTimer();
512518
}
513519

514520

515-
void ChannelWrap::CleanupTimer() {
516-
if (timer_handle_ == nullptr) return;
517-
518-
env()->CloseHandle(timer_handle_, [](uv_timer_t* handle) { delete handle; });
519-
timer_handle_ = nullptr;
520-
}
521-
522521
void ChannelWrap::ModifyActivityQueryCount(int count) {
523522
active_query_count_ += count;
524523
if (active_query_count_ < 0) active_query_count_ = 0;
@@ -566,6 +565,7 @@ void ChannelWrap::EnsureServers() {
566565
/* destroy channel and reset channel */
567566
ares_destroy(channel_);
568567

568+
CloseTimer();
569569
Setup();
570570
}
571571

0 commit comments

Comments
 (0)