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

fix potential race condition in DefaultResolver when discard_cache() #698

Open
wants to merge 1 commit into
base: release/0.7
Choose a base branch
from
Open
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
13 changes: 5 additions & 8 deletions net/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,12 @@ class DefaultResolver : public Resolver {
IPAddr addr;
IPAddrNode(IPAddr addr) : addr(addr) {}
};
using IPAddrList = intrusive_list<IPAddrNode>;
struct IPAddrList : public intrusive_list<IPAddrNode>, rwlock {
~IPAddrList() { delete_all(); }
};
public:
DefaultResolver(uint64_t cache_ttl, uint64_t resolve_timeout)
: dnscache_(cache_ttl), resolve_timeout_(resolve_timeout) {}
~DefaultResolver() {
for (auto it : dnscache_) {
((IPAddrList*)it->_obj)->delete_all();
}
dnscache_.clear();
}

IPAddr resolve(const char *host) override {
auto ctr = [&]() -> IPAddrList* {
auto addrs = new IPAddrList();
Expand All @@ -295,6 +290,7 @@ class DefaultResolver : public Resolver {
};
auto ips = dnscache_.borrow(host, ctr);
if (ips->empty()) LOG_ERRNO_RETURN(0, IPAddr(), "Domain resolution for ` failed", host);
scoped_rwlock _(*ips, RLOCK);
auto ret = ips->front();
ips->node = ret->next(); // access in round robin order
return ret->addr;
Expand All @@ -304,6 +300,7 @@ class DefaultResolver : public Resolver {

void discard_cache(const char *host, IPAddr ip) override {
auto ipaddr = dnscache_.borrow(host);
scoped_rwlock _(*ipaddr, WLOCK);
if (ip.undefined() || ipaddr->empty()) {
ipaddr->delete_all();
Copy link
Collaborator

Choose a reason for hiding this comment

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

recycle will destruct ipaddrlist so do not need to delete items right here, and also do not need a write lock in such condition.

ipaddr.recycle(true);
Expand Down
Loading