From 36fe3d2deba1a00b35e9643784fa2391ec23409d Mon Sep 17 00:00:00 2001 From: Tyler Kabana Date: Wed, 17 Jun 2026 15:33:36 -0700 Subject: [PATCH] Add cache clearing for caget-created channels in Buffer disconnection --- slac_timing/buffer.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/slac_timing/buffer.py b/slac_timing/buffer.py index a4110dc..670428f 100644 --- a/slac_timing/buffer.py +++ b/slac_timing/buffer.py @@ -1,4 +1,3 @@ -import gc import time as _time from abc import ABC, abstractmethod from typing import Optional @@ -73,7 +72,27 @@ def _disconnect_pvs(self) -> None: if self._pvs is not None: self._pvs.disconnect() self._pvs = None - gc.collect() # Force garbage collection to clean up PVs + self._clear_ca_cache() + + def _clear_ca_cache(self) -> None: + """Remove caget-created channels for this buffer from the CA cache.""" + import epics.ca + + ctx = epics.ca.current_context() + if ctx is None: + return + context_cache = epics.ca._cache.get(ctx) + if context_cache is None: + return + suffix = f"HST{self.number}" + stale = [name for name in context_cache if name.endswith(suffix)] + for name in stale: + entry = context_cache.pop(name, None) + if entry is not None and entry.chid is not None: + try: + epics.ca.clear_channel(entry.chid) + except Exception: + pass def is_reserved(self) -> bool: return self.number is not None and self.number != 0