@@ -758,23 +758,26 @@ def _open_connection_to_missing_shard(self, shard_id):
758758 )
759759 old_conn = None
760760 with self ._lock :
761- if self .is_shutdown :
762- conn .close ()
763- return
764- if conn .features .shard_id in self ._connections .keys ():
765- # Move the current connection to the trash and use the new one from now on
766- old_conn = self ._connections [conn .features .shard_id ]
767- log .debug (
768- "Replacing overloaded connection (%s) with (%s) for shard %i for host %s" ,
769- id (old_conn ),
770- id (conn ),
771- conn .features .shard_id ,
772- self .host
773- )
774- if self ._keyspace :
775- conn .set_keyspace_blocking (self ._keyspace )
761+ is_shutdown = self .is_shutdown
762+ if not is_shutdown :
763+ if conn .features .shard_id in self ._connections .keys ():
764+ # Move the current connection to the trash and use the new one from now on
765+ old_conn = self ._connections [conn .features .shard_id ]
766+ log .debug (
767+ "Replacing overloaded connection (%s) with (%s) for shard %i for host %s" ,
768+ id (old_conn ),
769+ id (conn ),
770+ conn .features .shard_id ,
771+ self .host
772+ )
773+ if self ._keyspace :
774+ conn .set_keyspace_blocking (self ._keyspace )
775+ self ._connections [conn .features .shard_id ] = conn
776+
777+ if is_shutdown :
778+ conn .close ()
779+ return
776780
777- self ._connections [conn .features .shard_id ] = conn
778781 if old_conn is not None :
779782 remaining = old_conn .in_flight - len (old_conn .orphaned_request_ids )
780783 if remaining == 0 :
@@ -794,10 +797,11 @@ def _open_connection_to_missing_shard(self, shard_id):
794797 remaining ,
795798 )
796799 with self ._lock :
797- if self .is_shutdown :
798- old_conn .close ()
799- else :
800+ is_shutdown = self .is_shutdown
801+ if not is_shutdown :
800802 self ._trash .add (old_conn )
803+ if is_shutdown :
804+ conn .close ()
801805 num_missing_or_needing_replacement = self .num_missing_or_needing_replacement
802806 log .debug (
803807 "Connected to %s/%i shards on host %s (%i missing or needs replacement)" ,
0 commit comments