Skip to content

Commit 3f0079c

Browse files
committed
fix(actor_hli): cleanup of the redis connections on shutdown
1 parent cc114e0 commit 3f0079c

4 files changed

Lines changed: 19 additions & 7 deletions

File tree

bec_lib/bec_lib/builtin_actor_hli.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@
1212
if TYPE_CHECKING:
1313
from bec_lib.client import BECClient
1414

15-
VAR_PREFIX = "_BuiltinActors"
16-
1715

1816
class ScanInterlockHli:
1917
def __init__(self, client: "BECClient", parent: "BuiltinActorHli") -> None:
2018
self._client = client
21-
self._parent = parent
22-
self._actor_name = "ScanInterlockActor"
2319
self._enabled = RedisConfigValue(
2420
connector=self._client.connector, endpoint=MessageEndpoints.scan_interlock_enabled()
2521
)
@@ -102,8 +98,17 @@ def clear_all(self):
10298
{"data": ScanInterlockModifyStateTableMessage(action="remove_all")},
10399
)
104100

101+
def shutdown(self):
102+
"""Unregister the config-value stream subscriptions from the connector."""
103+
self._enabled.unregister_all()
104+
self._trigger_setting.unregister_all()
105+
105106

106107
class BuiltinActorHli:
107108
def __init__(self, client: "BECClient") -> None:
108109
self._client = client
109110
self.scan_interlock = ScanInterlockHli(self._client, self)
111+
112+
def shutdown(self):
113+
"""Tear down builtin-actor client subscriptions (called on client shutdown)."""
114+
self.scan_interlock.shutdown()

bec_lib/bec_lib/client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ def __init__(
135135
if self._initialized:
136136
return
137137
self.__init_params: _InitParams = {
138-
"config": config if config is not None else ServiceConfig(config_name="client"),
139-
"connector_cls": connector_cls if connector_cls is not None else RedisConnector,
138+
"config": (config if config is not None else ServiceConfig(config_name="client")),
139+
"connector_cls": (connector_cls if connector_cls is not None else RedisConnector),
140140
"wait_for_server": wait_for_server,
141141
"prompt_for_acl": prompt_for_acl,
142142
}
@@ -164,6 +164,7 @@ def __init__(
164164
self._system_user = ""
165165
self.beamline_states = None
166166
self.messaging: MessagingContainer = None # type: ignore
167+
self.builtin_actors: BuiltinActorHli = None # type: ignore
167168

168169
def __new__(cls, *args, forced=False, **kwargs):
169170
if forced or BECClient._client is None:
@@ -336,6 +337,9 @@ def shutdown(self, per_thread_timeout_s: float | None = None):
336337
if self.history is not None:
337338
# pylint: disable=protected-access
338339
self.history._shutdown()
340+
if self.builtin_actors is not None:
341+
self.builtin_actors.shutdown()
342+
self.builtin_actors = None # type: ignore
339343
bec_logger.logger.remove()
340344
self.started = False
341345

bec_server/bec_server/actors/builtin_actor_manager.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222

2323
class ActorDict(dict):
24+
2425
def __setitem__(self, key: type[ActorType], value: tuple[ActorType, Thread, Event], /) -> None:
2526
return super().__setitem__(key, value)
2627

@@ -66,7 +67,7 @@ def _ping_clients(self, actor_name: str):
6667
)
6768

6869
def _interlock_enabled_changed(self, enabled: bool):
69-
self._start_actor(ScanInterlockActor) if enabled else self._stop_actor(ScanInterlockActor)
70+
(self._start_actor(ScanInterlockActor) if enabled else self._stop_actor(ScanInterlockActor))
7071

7172
def _start_all(self):
7273
if self._scan_interlock_enabled.value:
@@ -95,6 +96,7 @@ def _stop_actor(self, actor_class: type[ActorBase]):
9596
del actor
9697

9798
def shutdown(self):
99+
self._scan_interlock_enabled.unregister_all()
98100
for actor in list(self._actors_threads_and_stops):
99101
self._stop_actor(actor)
100102
self._client.shutdown()

bec_server/bec_server/actors/scan_interlock.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,6 @@ def run(self):
126126
self._unlock()
127127

128128
def stop(self, *_):
129+
self._restart_scan_on_lock.unregister_all()
129130
self._unlock()
130131
super().stop()

0 commit comments

Comments
 (0)