inbound_xfr() and exceptions

67 views
Skip to first unread message

Martin

unread,
Aug 30, 2022, 6:05:54 AM8/30/22
to dnspython-users
Hi.

I have a following simple try/except block:

try:
    dns.resolver.resolve('www.example.com', 'A')
except dns.exception.DNSException as err:
    print(f'ERROR: {err!r}')

In case the DNS server for example refuses the connection or connection times out, then the exception is caught and error message is printed. This works as expected. However, in case of inbound_xfr() the exceptions do not seem to be caught correctly. Example:

try:
    zone = dns.zone.Zone('example.net')
    dns.query.inbound_xfr('192.168.1.55', zone, port=53)
except dns.exception.DNSException as err:
    print(f'ERROR: {err!r}')

If the 192.168.1.55 drops traffic on TCP port 53, then DNSException is not caught and TimeoutError(https://docs.python.org/3/library/exceptions.html) is risen.

Is this an expected behavior? Or am I doing something wrong?

I'm using dnspython version 2.2.1.

thanks,
Martin

Bob Halley

unread,
Aug 30, 2022, 7:58:44 AM8/30/22
to dnspython-users
This situation happens if a socket connection attempt fails because the TCP handshake timed out.  For better or worse, dnspython does not wrap this exception when there is a timeout, so you get the TimeoutError.  This doesn't happen with dns.resolver.resolver() as it does a blanket "except Exception" catch around resolution attempts.  I probably would have wrapped this error if I had thought of it at the time, but I'm hesitant to change things now lest other code break.  The description of inbound_xfr() only says it "Raises on errors" so if you want to handle any error you should catch Exception instead.  If you want to handle just timeouts,  you should catch (dns.exception.Timeout, TimeoutError).

/Bob

Martin

unread,
Aug 30, 2022, 12:08:55 PM8/30/22
to dnspython-users
Hi Bob,

got it, thanks!


Martin
Reply all
Reply to author
Forward
0 new messages