Skip to content

Commit f7ee7af

Browse files
committed
Fix stale references in client routes handler
- Add type annotations to _ClientRoutesHandler.__init__ - Fix sleep() -> time.sleep() - Fix dep -> cp variable name - Add shutdown() method with _is_shutdown/_lock fields
1 parent 495bccc commit f7ee7af

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

cassandra/client_routes.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,12 @@ def __init__(self, config: 'ClientRoutesConfig', ssl_enabled: bool = False):
220220
if not isinstance(config, ClientRoutesConfig):
221221
raise TypeError("config must be a ClientRoutesConfig instance")
222222

223-
self.config = config
224-
self.ssl_enabled = ssl_enabled
225-
self._routes = _RouteStore()
226-
self._initial_contact_points = {dep.connection_id for dep in config.contact_points}
223+
self.config: ClientRoutesConfig = config
224+
self.ssl_enabled: bool = ssl_enabled
225+
self._routes: _RouteStore = _RouteStore()
226+
self._initial_contact_points: Set[str] = {cp.connection_id for cp in config.contact_points}
227+
self._is_shutdown: bool = False
228+
self._lock = threading.RLock()
227229

228230
def initialize(self, control_connection: 'ControlConnection') -> None:
229231
"""
@@ -304,7 +306,7 @@ def handle_control_connection_reconnect(self, control_connection: 'ControlConnec
304306
if attempt < max_attempts:
305307
log.warning("[client routes] Transient failure reloading routes "
306308
"(attempt %d/%d): %s", attempt, max_attempts, e)
307-
sleep(1)
309+
time.sleep(1)
308310
else:
309311
log.error("[client routes] Failed to reload routes after %d attempts: %s",
310312
max_attempts, e, exc_info=True)
@@ -395,3 +397,14 @@ def resolve_host(self, host_id: uuid.UUID) -> Optional[Tuple[str, int]]:
395397
return None
396398

397399
return route.address, route.port
400+
401+
def shutdown(self) -> None:
402+
"""
403+
Shutdown the handler and release resources.
404+
"""
405+
with self._lock:
406+
if self._is_shutdown:
407+
return
408+
409+
self._is_shutdown = True
410+
log.info("[client routes] Handler shutdown")

0 commit comments

Comments
 (0)