Skip to content

Commit cb45fc5

Browse files
committed
Add a lock to the controllers dictionary when performing the clenaup of dormant controllers and split the dormant controller parsing and deletion into separate for loops
1 parent 432c50b commit cb45fc5

File tree

1 file changed

+14
-6
lines changed
  • src/murfey/instrument_server

1 file changed

+14
-6
lines changed

src/murfey/instrument_server/api.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from functools import partial
88
from logging import getLogger
99
from pathlib import Path
10+
from threading import Lock
1011
from typing import Annotated, Any, Optional
1112
from urllib.parse import urlparse
1213

@@ -31,6 +32,7 @@
3132
watchers: dict[str | int, MultigridDirWatcher] = {}
3233
rsyncers: dict[str, RSyncer] = {}
3334
controllers: dict[int, MultigridController] = {}
35+
controller_lock = Lock()
3436
data_collection_parameters: dict = {}
3537
tokens = {}
3638

@@ -145,22 +147,26 @@ def check_token(session_id: MurfeySessionID):
145147
def setup_multigrid_watcher(
146148
session_id: MurfeySessionID, watcher_spec: MultigridWatcherSpec
147149
):
150+
# Remove dormant controllers from memory
151+
with controller_lock:
152+
controllers_to_remove = [
153+
sid for sid, controller in controllers.items() if controller.dormant
154+
]
155+
for sid in controllers_to_remove:
156+
del controllers[sid]
157+
148158
# Return 'True' if controllers are already set up
149159
if controllers.get(session_id) is not None:
150160
return {"success": True}
151161

152-
label = watcher_spec.label
153-
for sid, controller in controllers.items():
154-
if controller.dormant:
155-
del controllers[sid]
156-
157162
# Load machine config as dictionary
158163
machine_config: dict[str, Any] = requests.get(
159164
f"{_get_murfey_url()}{url_path_for('session_control.router', 'machine_info_by_instrument', instrument_name=sanitise_nonpath(watcher_spec.instrument_name))}",
160165
headers={"Authorization": f"Bearer {tokens[session_id]}"},
161166
).json()
162167

163-
# Set up the multigrid controll controller
168+
# Set up the multigrid controller
169+
label = watcher_spec.label
164170
controllers[session_id] = MultigridController(
165171
[],
166172
watcher_spec.visit,
@@ -268,7 +274,9 @@ def finalise_rsyncer(session_id: MurfeySessionID, rsyncer_source: RsyncerSource)
268274
@router.post("/sessions/{session_id}/finalise_session")
269275
def finalise_session(session_id: MurfeySessionID):
270276
watchers[session_id].request_stop()
277+
logger.debug(f"Stop request sent to multigrid watcher for session {session_id}")
271278
controllers[session_id].finalise()
279+
logger.debug(f"Stop orders sent to multigrid controller for session {session_id} ")
272280
return {"success": True}
273281

274282

0 commit comments

Comments
 (0)