Skip to content

Commit

Permalink
Fix bug : DNSResolver::OnTimeout do not clear the timer. see #14
Browse files Browse the repository at this point in the history
  • Loading branch information
zieckey committed Mar 18, 2017
1 parent b72a4c0 commit 8af69d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
22 changes: 13 additions & 9 deletions evpp/dns_resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ DNSResolver::DNSResolver(EventLoop* evloop, const std::string& h, Duration timeo

DNSResolver::~DNSResolver() {
LOG_INFO << "DNSResolver::~DNSResolver tid=" << std::this_thread::get_id() << " this=" << this;

assert(dnsbase_ == nullptr);
assert(!timer_);
}

void DNSResolver::Start() {
Expand Down Expand Up @@ -82,9 +82,8 @@ void DNSResolver::OnTimeout() {
#if LIBEVENT_VERSION_NUMBER >= 0x02001500
evdns_getaddrinfo_cancel(dns_req_);
dns_req_ = nullptr;
//evdns_base_free(dnsbase_, 0);
//dnsbase_ = nullptr;
#endif
ClearTimer();
if (functor_) {
functor_(this->addrs_);
}
Expand All @@ -95,9 +94,8 @@ void DNSResolver::OnCanceled() {
#if LIBEVENT_VERSION_NUMBER >= 0x02001500
evdns_getaddrinfo_cancel(dns_req_);
dns_req_ = nullptr;
//evdns_base_free(dnsbase_, 0);
//dnsbase_ = nullptr;
#endif
timer_.reset();
}


Expand Down Expand Up @@ -130,7 +128,7 @@ void DNSResolver::AsyncDNSResolve() {
void DNSResolver::OnResolved(int errcode, struct addrinfo* addr) {
if (errcode != 0) {
if (errcode != EVUTIL_EAI_CANCEL) {
timer_->Cancel();
ClearTimer();
LOG_ERROR << "dns resolve failed, "
<< ", error code: " << errcode
<< ", error msg: " << evutil_gai_strerror(errcode);
Expand All @@ -155,7 +153,7 @@ void DNSResolver::OnResolved(int errcode, struct addrinfo* addr) {
LOG_INFO << "delete dns base";
evdns_base_free(dnsbase_, 0);
dnsbase_ = nullptr;

ClearTimer();
if (functor_) {
functor_(this->addrs_);
}
Expand All @@ -178,8 +176,7 @@ void DNSResolver::OnResolved(int errcode, struct addrinfo* addr) {
LOG_TRACE << host_ << " resolved a ip=" << inet_ntoa(a->sin_addr);
}
evutil_freeaddrinfo(addr);
timer_->SetCancelCallback(TimerEventWatcher::Handler());
timer_->Cancel();
ClearTimer();

LOG_INFO << "delete dns base";
evdns_base_free(dnsbase_, 0); //TODO Do we need to free dns_req_
Expand All @@ -197,6 +194,13 @@ void DNSResolver::OnResolved(int errcode, struct addrinfo* addr, void* arg) {
//DNSResolver* dns = reinterpret_cast<DNSResolver*>(arg);
//dns->OnResolved(errcode, addr);
}

void DNSResolver::ClearTimer() {
timer_->SetCancelCallback(TimerEventWatcher::Handler());
timer_->Cancel();
timer_.reset();
}

#endif

}
1 change: 1 addition & 0 deletions evpp/dns_resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class EVPP_EXPORT DNSResolver : public std::enable_shared_from_this<DNSResolver>
void OnTimeout();
void OnCanceled();
void OnResolved(int errcode, struct addrinfo* addr);
void ClearTimer();
static void OnResolved(int errcode, struct addrinfo* addr, void* arg);
private:
EventLoop* loop_;
Expand Down

0 comments on commit 8af69d5

Please sign in to comment.