Skip to content

Commit

Permalink
prevent async resolver infinite attempts if caller does not use a tim…
Browse files Browse the repository at this point in the history
…eout scope
  • Loading branch information
rthalley committed May 17, 2020
1 parent cea118a commit bf4c6c6
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions dns/trio/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
_udp = dns.trio.query.udp
_stream = dns.trio.query.stream

class TooManyAttempts(dns.exception.DNSException):
"""A resolution had too many unsuccessful attempts."""

class Resolver(dns.resolver.Resolver):

async def resolve(self, qname, rdtype=dns.rdatatype.A,
Expand Down Expand Up @@ -126,6 +129,7 @@ async def resolve(self, qname, rdtype=dns.rdatatype.A,
# to include them in Answer
nameserver_answered = None
port_answered = None
loops = 0
while response is None:
if len(nameservers) == 0:
raise NoNameservers(request=request, errors=errors)
Expand Down Expand Up @@ -227,6 +231,11 @@ async def resolve(self, qname, rdtype=dns.rdatatype.A,
#
# All nameservers failed!
#
# Do not loop forever if caller hasn't used a timeout
# scope.
loops += 1
if loops >= 5:
raise TooManyAttempts
if len(nameservers) > 0:
#
# But we still have servers to try. Sleep a bit
Expand Down

0 comments on commit bf4c6c6

Please sign in to comment.