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 DNSCache race-condition #2621

Merged
merged 1 commit into from
Dec 27, 2017
Merged
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
1 change: 1 addition & 0 deletions CHANGES/2620.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed race-condition for iterating addresses from the DNSCache.
9 changes: 5 additions & 4 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,10 +578,11 @@ def clear(self):
self._timestamps.clear()

def next_addrs(self, host):
# Return an iterator that will get at maximum as many addrs
# there are for the specific host starting from the last
# not itereated addr.
return islice(self._addrs_rr[host], len(self._addrs[host]))
loop = self._addrs_rr[host]
addrs = list(islice(loop, len(self._addrs[host])))
# Consume one more element to shift internal state of `cycle`
next(loop)
return addrs

def expired(self, host):
if self._ttl is None:
Expand Down
28 changes: 20 additions & 8 deletions tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1959,15 +1959,27 @@ async def test_expired_ttl(self, loop):
assert dns_cache_table.expired('localhost')

def test_next_addrs(self, dns_cache_table):
dns_cache_table.add('foo', ['127.0.0.1', '127.0.0.2'])
dns_cache_table.add('foo', ['127.0.0.1', '127.0.0.2', '127.0.0.3'])

# max elements returned are the full list of addrs
addrs = list(dns_cache_table.next_addrs('foo'))
assert addrs == ['127.0.0.1', '127.0.0.2']

# different calls to next_addrs return the hosts using
# Each calls to next_addrs return the hosts using
# a round robin strategy.
addrs = dns_cache_table.next_addrs('foo')
assert next(addrs) == '127.0.0.1'
assert addrs == ['127.0.0.1', '127.0.0.2', '127.0.0.3']

addrs = dns_cache_table.next_addrs('foo')
assert addrs == ['127.0.0.2', '127.0.0.3', '127.0.0.1']

addrs = dns_cache_table.next_addrs('foo')
assert addrs == ['127.0.0.3', '127.0.0.1', '127.0.0.2']

addrs = dns_cache_table.next_addrs('foo')
assert addrs == ['127.0.0.1', '127.0.0.2', '127.0.0.3']

def test_next_addrs_single(self, dns_cache_table):
dns_cache_table.add('foo', ['127.0.0.1'])

addrs = dns_cache_table.next_addrs('foo')
assert addrs == ['127.0.0.1']

addrs = dns_cache_table.next_addrs('foo')
assert next(addrs) == '127.0.0.2'
assert addrs == ['127.0.0.1']