diff --git a/lib/network.py b/lib/network.py index d6349711f..1c25fb64a 100644 --- a/lib/network.py +++ b/lib/network.py @@ -32,8 +32,12 @@ from collections import defaultdict import threading import socket import json +import sys +import dns +import dns.resolver import socks + from . import util from . import bitcoin from .bitcoin import * @@ -413,7 +417,23 @@ class Network(util.DaemonThread): socket.getaddrinfo = lambda *args: [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))] else: socket.socket = socket._socketobject - socket.getaddrinfo = socket._getaddrinfo + if sys.platform == 'win32': + # on Windows, socket.getaddrinfo takes a mutex, and might hold it for up to 10 seconds + # when dns-resolving. to speed it up drastically, we resolve dns ourselves, outside that lock + def fast_getaddrinfo(host, *args, **kwargs): + try: + if str(host) not in ('localhost', 'localhost.',): + answers = dns.resolver.query(host) + addr = str(answers[0]) + else: + addr = host + except: + raise socket.gaierror(11001, 'getaddrinfo failed') + else: + return socket._getaddrinfo(addr, *args, **kwargs) + socket.getaddrinfo = fast_getaddrinfo + else: + socket.getaddrinfo = socket._getaddrinfo def start_network(self, protocol, proxy): assert not self.interface and not self.interfaces