Skip to content

Commit 2b0a359

Browse files
committed
Cleanup stale camera workers on start/shutdown
Detect and remove stale camera worker/thread state when starting cameras and during shutdown. _start_camera now checks the existing thread for a cam_id and calls _cleanup_camera if the worker exists but the thread is missing or not running, and updates the log message to indicate the camera is already running. The shutdown loop likewise invokes _cleanup_camera for None or not-running threads and after successful quit/terminate waits to ensure worker/thread entries are cleared. These changes prevent stale entries from blocking restarts and ensure cleaner shutdown behavior.
1 parent 91dff83 commit 2b0a359

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

dlclivegui/services/multi_camera_controller.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,12 @@ def start(self, camera_settings: list[CameraSettings]) -> None:
172172
def _start_camera(self, settings: CameraSettings) -> None:
173173
"""Start a single camera."""
174174
cam_id = get_camera_id(settings)
175+
existing_thread = self._threads.get(cam_id)
176+
if cam_id in self._workers and (existing_thread is None or not existing_thread.isRunning()):
177+
LOGGER.warning(f"Stale camera worker/thread found for {cam_id}, cleaning up")
178+
self._cleanup_camera(cam_id)
175179
if cam_id in self._workers:
176-
LOGGER.warning(f"Camera {cam_id} already has a worker")
180+
LOGGER.warning(f"Camera {cam_id} is already running, skipping start")
177181
return
178182

179183
# Normalize and store the dataclass once
@@ -227,12 +231,15 @@ def stop(self, wait: bool = True) -> None:
227231
still_running: list[str] = []
228232
for cam_id, thread in list(self._threads.items()):
229233
if thread is None:
234+
self._cleanup_camera(cam_id)
230235
continue
231236
if not thread.isRunning():
237+
self._cleanup_camera(cam_id)
232238
continue
233239

234240
thread.quit()
235241
if thread.wait(QUIT_WAIT_MS):
242+
self._cleanup_camera(cam_id)
236243
continue # Clean exit
237244

238245
LOGGER.error(
@@ -243,6 +250,7 @@ def stop(self, wait: bool = True) -> None:
243250

244251
thread.terminate()
245252
if thread.wait(TERMINATE_WAIT_MS):
253+
self._cleanup_camera(cam_id)
246254
continue # Terminated successfully
247255

248256
LOGGER.critical(

0 commit comments

Comments
 (0)