120120from lib .core .settings import MAX_NUMBER_OF_THREADS
121121from lib .core .settings import NULL
122122from lib .core .settings import PARAMETER_SPLITTING_REGEX
123+ from lib .core .settings import PRECONNECT_CANDIDATE_TIMEOUT
123124from lib .core .settings import PROBLEMATIC_CUSTOM_INJECTION_PATTERNS
124125from lib .core .settings import SITE
125126from lib .core .settings import SOCKET_PRE_CONNECT_QUEUE_SIZE
@@ -1039,7 +1040,7 @@ def _():
10391040 s = socket .socket (family , type , proto )
10401041 s ._connect (address )
10411042 with kb .locks .socket :
1042- socket ._ready [key ].append (s ._sock )
1043+ socket ._ready [key ].append (( s ._sock , time . time ()) )
10431044 except KeyboardInterrupt :
10441045 break
10451046 except :
@@ -1054,9 +1055,17 @@ def connect(self, address):
10541055 with kb .locks .socket :
10551056 if key not in socket ._ready :
10561057 socket ._ready [key ] = []
1057- if len (socket ._ready [key ]) > 0 :
1058- self ._sock = socket ._ready [key ].pop (0 )
1059- found = True
1058+ while len (socket ._ready [key ]) > 0 :
1059+ candidate , created = socket ._ready [key ].pop (0 )
1060+ if (time .time () - created ) < PRECONNECT_CANDIDATE_TIMEOUT :
1061+ self ._sock = candidate
1062+ found = True
1063+ break
1064+ else :
1065+ try :
1066+ candidate .close ()
1067+ except socket .error :
1068+ pass
10601069
10611070 if not found :
10621071 self ._connect (address )
@@ -2282,6 +2291,7 @@ def _setTorHttpProxySettings():
22822291 infoMsg = "setting Tor HTTP proxy settings"
22832292 logger .info (infoMsg )
22842293
2294+ s = None
22852295 found = None
22862296
22872297 for port in (DEFAULT_TOR_HTTP_PORTS if not conf .torPort else (conf .torPort ,)):
@@ -2293,7 +2303,8 @@ def _setTorHttpProxySettings():
22932303 except socket .error :
22942304 pass
22952305
2296- s .close ()
2306+ if s :
2307+ s .close ()
22972308
22982309 if found :
22992310 conf .proxy = "http://%s:%d" % (LOCALHOST , found )
0 commit comments