@@ -613,23 +613,8 @@ def return_connection(self, connection, stream_was_orphaned=False):
613613 if not connection .signaled_error :
614614 log .debug ("Defunct or closed connection (%s) returned to pool, potentially "
615615 "marking host %s as down" , id (connection ), self .host )
616- with self .host .lock :
617- endpoint_matches = _endpoints_match (
618- self ._session .cluster , self .host .endpoint , self .endpoint )
619- host_is_current = (endpoint_matches and
620- _host_is_current_for_endpoint (
621- self ._session .cluster , self .host ,
622- self .endpoint ))
623- if not endpoint_matches :
624- log .debug ("Ignoring stale connection failure for host %s; endpoint changed from %s" ,
625- self .host , self .endpoint )
626- stale_endpoint_failure = True
627- elif not host_is_current :
628- log .debug ("Ignoring stale connection failure for host %s; endpoint reassigned from %s" ,
629- self .host , self .endpoint )
630- stale_endpoint_failure = True
631- else :
632- is_down = self .host .signal_connection_failure (connection .last_error )
616+ is_down , stale_endpoint_failure = \
617+ self ._signal_connection_failure_if_current (connection )
633618 connection .signaled_error = True
634619
635620 if stale_endpoint_failure :
@@ -683,6 +668,52 @@ def on_orphaned_stream_released(self):
683668 with self ._stream_available_condition :
684669 self ._stream_available_condition .notify ()
685670
671+ def _is_current_pool_for_endpoint_locked (self , expected_endpoint ):
672+ pools = getattr (self ._session , "_pools" , None )
673+ if not isinstance (pools , dict ):
674+ return True
675+
676+ for pool_host , host_pool in pools .items ():
677+ if pool_host is not self .host or host_pool is not self :
678+ continue
679+ pool_endpoint = getattr (host_pool , 'endpoint' , None )
680+ if pool_endpoint is None :
681+ pool_endpoint = host_pool .host .endpoint
682+ return _endpoints_match (
683+ self ._session .cluster , pool_endpoint , expected_endpoint )
684+ return False
685+
686+ def _signal_connection_failure_if_current (self , connection ):
687+ session_lock = getattr (self ._session , "_lock" , None )
688+ if session_lock is None :
689+ with self .host .lock :
690+ return self ._signal_connection_failure_locked (connection )
691+
692+ with session_lock :
693+ with self .host .lock :
694+ if not self ._is_current_pool_for_endpoint_locked (self .endpoint ):
695+ log .debug ("Ignoring stale connection failure for host %s; pool was replaced for %s" ,
696+ self .host , self .endpoint )
697+ return False , True
698+ return self ._signal_connection_failure_locked (connection )
699+
700+ def _signal_connection_failure_locked (self , connection ):
701+ endpoint_matches = _endpoints_match (
702+ self ._session .cluster , self .host .endpoint , self .endpoint )
703+ host_is_current = (endpoint_matches and
704+ _host_is_current_for_endpoint (
705+ self ._session .cluster , self .host ,
706+ self .endpoint ))
707+ if not endpoint_matches :
708+ log .debug ("Ignoring stale connection failure for host %s; endpoint changed from %s" ,
709+ self .host , self .endpoint )
710+ return False , True
711+ if not host_is_current :
712+ log .debug ("Ignoring stale connection failure for host %s; endpoint reassigned from %s" ,
713+ self .host , self .endpoint )
714+ return False , True
715+ return self .host .signal_connection_failure (connection .last_error ), False
716+
686717 def _remove_stale_pool (self , expected_endpoint ):
687718 future = self ._session .remove_pool (
688719 self .host , expected_host = self .host ,
0 commit comments