How catching dns.exception.Timeout ?

66 views
Skip to first unread message

Axel Rau

unread,
Jun 22, 2021, 11:10:42 AM6/22/21
to dnspyth...@googlegroups.com
Hi,

I have:

try:
print('createZoneFromName: {}'.format(fqdn))
zoneName = str(dns.resolver.zone_for_name(fqdn))
print('createZoneFromName: {} done'.format(fqdn))
except dns.name.EmptyLabel:
print('Empty label: name={}, dtype={}'.format(name, dtype))
except Exception():
return

but I get:

File "/usr/local/py_env/zad/lib/python3.9/site-packages/dns/resolver.py", line 1373, in zone_for_name
answer = resolver.resolve(name, dns.rdatatype.SOA, rdclass, tcp)
File "/usr/local/py_env/zad/lib/python3.9/site-packages/dns/resolver.py", line 1176, in resolve
timeout = self._compute_timeout(start, lifetime)
File "/usr/local/py_env/zad/lib/python3.9/site-packages/dns/resolver.py", line 997, in _compute_timeout
raise Timeout(timeout=duration)
dns.exception.Timeout: The DNS operation timed out after 5.002664804458618 seconds

Any help appreciated,
Axel
---
PGP-Key: CDE74120 ☀ computing @ chaos claudius

signature.asc

Axel Rau

unread,
Jun 22, 2021, 11:14:19 AM6/22/21
to dnspyth...@googlegroups.com
Even replacing Exception by BaseException() does not help.

Axel
signature.asc

Bob Halley

unread,
Jun 22, 2021, 11:33:58 AM6/22/21
to dnspython-users
You should not add parentheses to the exception name, i.e. you want

except  Exception:

or 

except dns.exception.Timeout:

and not

except Exception():

Axel Rau

unread,
Jun 23, 2021, 7:57:12 AM6/23/21
to dnspyth...@googlegroups.com


> Am 22.06.2021 um 17:14 schrieb Axel Rau <Axel...@chaos1.de>:
>
> Even replacing Exception by BaseException() does not help.

It seems that resolver.zone_for_name sometimes failes to find a IPv6 reverse zone, which causes the timeout:
- - -
%createZoneFromName/zone_for_name name=2001:4bd8:0:104:20a:e4ff:fe80:3138, fqdn=8.3.1.3.0.8.e.f.f.f.4.e.a.0.2.0.4.0.1.0.0.0.0.0.8.d.b.4.1.0.0.2.ip6.arpa.,
because Timeout [The DNS operation timed out after 5.002438068389893 seconds]
- - -
- - -
def createZoneFromName(self, dtype, name):

if dtype in ('NS', 'MX', 'CNAME', 'DNAME', 'PTR', 'SRV'): # a domain fqdn
if name[-1] == '.': # already absolute?
fqdn = name
else: # no, relative - make absolute
fqdn = name + '.' + self.zone_name
if len(domainZones.keys()) > zad.common.MAX_ZONES:
return
elif dtype == 'A': # address
if len(ip4Zones.keys()) > zad.common.MAX_ZONES:
return
fqdn = dns.reversename.from_address(name)
elif dtype == 'AAAA': # address
if len(ip6Zones.keys()) > zad.common.MAX_ZONES:
return
fqdn = dns.reversename.from_address(name)
else: # ignore others
return
try:
zoneName = str(dns.resolver.zone_for_name(fqdn))
print('createZoneFromName: name={}, fqdn={} OK: zone={}'.format(name, fqdn, zoneName))
except dns.name.EmptyLabel:
print('Empty label: name={}, dtype={}'.format(name, dtype))
except BaseException:
print('%createZoneFromName/zone_for_name name={}, fqdn={},\n because {} [{}]'.
format(name, fqdn,
sys.exc_info()[0].__name__,
str(sys.exc_info()[1])))
return
- - -
Is the cause a missing delegation or a bug?
Can you give me a tip to filter such IPs and not try to find there zone?

Thanks, Axel
signature.asc

Bob Halley

unread,
Jun 23, 2021, 9:05:41 AM6/23/21
to dnspython-users
It's not a missing delegation or bug, as when I run the code with that domain name things work.  Timeouts are a common occurrence in the DNS, they can have many causes, and can be very ephemeral.  In general, there's no good way to predict which queries will timeout.

Axel Rau

unread,
Jun 23, 2021, 9:55:03 AM6/23/21
to dnspyth...@googlegroups.com


Am 23.06.2021 um 15:05 schrieb Bob Halley <dnsp...@gmail.com>:

It's not a missing delegation or bug, as when I run the code with that domain name things work.  Timeouts are a common occurrence in the DNS, they can have many causes, and can be very ephemeral.  In general, there's no good way to predict which queries will timeout.

Moving away from cloudflaire and using my local resolver, improves things.
Querying one zone too often may cause rate limiting.
I try that in a loop with a sleep of 5s.
signature.asc
Reply all
Reply to author
Forward
0 new messages