@@ -80,9 +80,6 @@ def socket(self):
8080 self .sock = self .socket_factory ()
8181 return self .sock
8282
83- def close (self ):
84- self .sock = None
85-
8683 def return_socket (self ):
8784 if self .sock is not None :
8885 self .sockets .append (self .sock )
@@ -339,10 +336,7 @@ def __find_master(self):
339336 sock .settimeout (_CONNECT_TIMEOUT )
340337 sock .connect ((host , port ))
341338 sock .settimeout (self .__network_timeout )
342- try :
343- master = self .__master (sock )
344- except ConnectionFailure , e :
345- raise AutoReconnect (str (e ))
339+ master = self .__master (sock )
346340 if master is True :
347341 self .__host = host
348342 self .__port = port
@@ -363,9 +357,6 @@ def __find_master(self):
363357 "but that's not configured" %
364358 ((host , port ), master ))
365359 except socket .error , e :
366- exctype , value = sys .exc_info ()[:2 ]
367- if len (self .__nodes ) == 1 :
368- raise ConnectionFailure (e )
369360 continue
370361 finally :
371362 if sock is not None :
@@ -476,7 +467,7 @@ def _send_message(self, message, with_last_error=False):
476467 response = self .__receive_message_on_socket (1 , request_id , sock )
477468 self .__check_response_to_last_error (response )
478469 except (ConnectionFailure , socket .error ), e :
479- self .__pool . close ()
470+ self ._reset ()
480471 raise AutoReconnect (str (e ))
481472
482473 def __receive_data_on_socket (self , length , sock ):
@@ -487,10 +478,7 @@ def __receive_data_on_socket(self, length, sock):
487478 """
488479 message = ""
489480 while len (message ) < length :
490- try :
491- chunk = sock .recv (length - len (message ))
492- except socket .error , e :
493- raise ConnectionFailure (e )
481+ chunk = sock .recv (length - len (message ))
494482 if chunk == "" :
495483 raise ConnectionFailure ("connection closed" )
496484 message += chunk
@@ -517,7 +505,6 @@ def __send_and_receive(self, message, sock):
517505 sock .sendall (data )
518506 return self .__receive_message_on_socket (1 , request_id , sock )
519507
520- __hack_socket_lock = threading .Lock ()
521508 # we just ignore _must_use_master here: it's only relavant for
522509 # MasterSlaveConnection instances.
523510 def _send_message_with_response (self , message ,
@@ -530,18 +517,16 @@ def _send_message_with_response(self, message,
530517 - `message`: (request_id, data) pair making up the message to send
531518 """
532519 # hack so we can do find_master on a specific socket...
533- if _sock :
534- self .__hack_socket_lock .acquire ()
535- try :
536- return self .__send_and_receive (message , _sock )
537- finally :
538- self .__hack_socket_lock .release ()
520+ reset = False
521+ if _sock is None :
522+ reset = True
523+ _sock = self .__pool .socket ()
539524
540- sock = self .__pool .socket ()
541525 try :
542- return self .__send_and_receive (message , sock )
526+ return self .__send_and_receive (message , _sock )
543527 except (ConnectionFailure , socket .error ), e :
544- self .__pool .close ()
528+ if reset :
529+ self ._reset ()
545530 raise AutoReconnect (str (e ))
546531
547532 def start_request (self ):
@@ -566,6 +551,11 @@ def end_request(self):
566551 sure that :meth:`end_request` is not called in the middle of a
567552 sequence of operations in which ordering is important. This
568553 could lead to unexpected results.
554+
555+ One important case is when a thread is dying permanently. It
556+ is best to call :meth:`end_request` when you know a thread is
557+ finished, as otherwise its :class:`~socket.socket` will not be
558+ reclaimed.
569559 """
570560 self .__pool .return_socket ()
571561
0 commit comments