Home > Software design >  DNSPython asynchronous resolve times out but synchronous resolve doesn't
DNSPython asynchronous resolve times out but synchronous resolve doesn't

Time:12-07

I performed the tests on Python 3.9.7 x64, dnspython 2.1.0 and Windows 10 21H1.

The problem is very simple:

dns.resolver.Resolver() works normally but not dns.asyncresolver.Resolver(), they are supposed to have the same API, however while the former does indeed return results, the latter always times out.

Example:

In [55]: import dns
    ...: sequential_resolver = dns.resolver.Resolver()
    ...: sequential_resolver.nameservers = ['8.8.8.8']

In [56]: ans = sequential_resolver.resolve('www.google.com')

In [57]: ans[0].address
Out[57]: '162.125.18.129'

In [58]: from dns.asyncresolver import Resolver

In [59]: resolver = Resolver()

In [60]: resolver.nameservers = ['8.8.8.8']

In [61]: asyncio.run(resolver.resolve('www.google.com'))
---------------------------------------------------------------------------
Timeout                                   Traceback (most recent call last)
<ipython-input-61-4971055f7c5e> in <module>
----> 1 asyncio.run(resolver.resolve('www.google.com'))

C:\Program Files\Python39\lib\asyncio\runners.py in run(main, debug)
     42         if debug is not None:
     43             loop.set_debug(debug)
---> 44         return loop.run_until_complete(main)
     45     finally:
     46         try:

C:\Program Files\Python39\lib\asyncio\base_events.py in run_until_complete(self, future)
    640             raise RuntimeError('Event loop stopped before Future completed.')
    641
--> 642         return future.result()
    643
    644     def stop(self):

C:\Program Files\Python39\lib\site-packages\dns\asyncresolver.py in resolve(self, qname, rdtype, rdclass, tcp, source, raise_on_no_answer, source_port, lifetime, search, backend)
     72                 if backoff:
     73                     await backend.sleep(backoff)
---> 74                 timeout = self._compute_timeout(start, lifetime)
     75                 try:
     76                     if dns.inet.is_address(nameserver):

C:\Program Files\Python39\lib\site-packages\dns\resolver.py in _compute_timeout(self, start, lifetime)
    995                 now = start
    996         if duration >= lifetime:
--> 997             raise Timeout(timeout=duration)
    998         return min(lifetime - duration, self.timeout)
    999

Timeout: The DNS operation timed out after 5.407369613647461 seconds

I can assure you this issue is not caused by my physical broadband connection:

PS C:\Windows\System32> ping 8.8.8.8

Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time=66ms TTL=48
Reply from 8.8.8.8: bytes=32 time=66ms TTL=48
Reply from 8.8.8.8: bytes=32 time=66ms TTL=48
Reply from 8.8.8.8: bytes=32 time=67ms TTL=48

Ping statistics for 8.8.8.8:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 66ms, Maximum = 67ms, Average = 66ms

How can this be solved?

CodePudding user response:

It looks like there's a bug in dnspython, but the fix doesn't seem to be released on PyPi yet.

The best thing I can advise is to install the version from Github:

pip install -U https://github.com/rthalley/dnspython/archive/master.zip

With it your code will work.

  • Related