Browse Source

Merge pull request #4422 from SomberNight/fix_4421

socket.getaddrinfo on win32: do our own dns resolution outside the lock
3.2.x
ThomasV 7 years ago
committed by GitHub
parent
commit
5a75ce74d7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      lib/network.py

22
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

Loading…
Cancel
Save