From 9082b89082128f689469499c37cad882db5ef3b9 Mon Sep 17 00:00:00 2001 From: Sofia Donato Ferreira Date: Tue, 28 Jan 2025 08:59:57 -0300 Subject: [PATCH 1/2] refactor: change some occurences of _finished(success=True) to set_finished The prior is deprecated for some time already, replaced by the former. Signed-off-by: Sofia Donato Ferreira --- ophyd/signal.py | 4 ++-- ophyd/status.py | 11 ++++------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/ophyd/signal.py b/ophyd/signal.py index b3bbae302..d2267f433 100644 --- a/ophyd/signal.py +++ b/ophyd/signal.py @@ -225,7 +225,7 @@ def trigger(self): # NOTE: this is a no-op that exists here for bluesky purposes # it may need to be moved in the future d = Status(self) - d._finished() + d.set_finished() return d def wait_for_connection(self, timeout=0.0): @@ -2247,7 +2247,7 @@ def set(self, value, *, timeout=DEFAULT_WRITE_TIMEOUT, settle_time=None): st = Status(self, timeout=timeout, settle_time=settle_time) def put_callback(**kwargs): - st._finished(success=True) + st.set_finished() self.put(value, use_complete=True, callback=put_callback) return st diff --git a/ophyd/status.py b/ophyd/status.py index 428eb47cb..689b5d0ac 100644 --- a/ophyd/status.py +++ b/ophyd/status.py @@ -2,7 +2,6 @@ import threading import time from collections import deque -from functools import partial from logging import LoggerAdapter from warnings import warn @@ -599,7 +598,7 @@ def inner(status): elif l_success and r_success and l_done and r_done: # Both are done, successfully. - self._finished(success=True) + self.set_finished() # Else one is done, successfully, and we wait for #2, # when this function will be called again. @@ -793,7 +792,7 @@ def check_value(self, *args, **kwargs): # If successfull indicate completion if success: - self._finished(success=True) + self.set_finished() def set_finished(self): """ @@ -863,9 +862,7 @@ def __init__( f"Stability time ({stability_time}) must be less than full status timeout ({timeout})" ) self._stability_time = stability_time - self._stable_timer = threading.Timer( - self._stability_time, partial(self._finished, success=True) - ) + self._stable_timer = threading.Timer(self._stability_time, self.set_finished) # Start timeout thread in the background super().__init__( @@ -891,7 +888,7 @@ def check_value(self, *args, **kwargs): else: self._stable_timer.cancel() self._stable_timer = threading.Timer( - self._stability_time, partial(self._finished, success=True) + self._stability_time, self.set_finished ) # Do not fail silently From d7d9041c5397ddc1c424dd00245cf929afa18dfa Mon Sep 17 00:00:00 2001 From: Sofia Donato Ferreira Date: Tue, 28 Jan 2025 09:02:21 -0300 Subject: [PATCH 2/2] feat: replace usages of _finished(success=False) with set_exception This change both removes usage of a deprecate function call, and improves debugging of exceptions in the code. Signed-off-by: Sofia Donato Ferreira --- ophyd/signal.py | 18 ++++++++++++------ ophyd/status.py | 4 ++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/ophyd/signal.py b/ophyd/signal.py index d2267f433..6e9591444 100644 --- a/ophyd/signal.py +++ b/ophyd/signal.py @@ -394,10 +394,12 @@ def set(self, value, *, timeout=None, settle_time=None, **kwargs): ) def set_thread(): + _exception = None + try: self._set_and_wait(value, timeout, **kwargs) - except TimeoutError: - success = False + except TimeoutError as e: + _exception = e self.log.warning( "%s: _set_and_wait(value=%s, timeout=%s, atol=%s, rtol=%s, kwargs=%s)", self.name, @@ -407,8 +409,8 @@ def set_thread(): self.rtolerance, kwargs, ) - except Exception: - success = False + except Exception as e: + _exception = e self.log.exception( "%s: _set_and_wait(value=%s, timeout=%s, atol=%s, rtol=%s, kwargs=%s)", self.name, @@ -419,7 +421,6 @@ def set_thread(): kwargs, ) else: - success = True self.log.debug( "%s: _set_and_wait(value=%s, timeout=%s, atol=%s, rtol=%s, kwargs=%s) succeeded => %s", self.name, @@ -439,7 +440,12 @@ def set_thread(): th = self._set_thread # these two must be in this order to avoid a race condition self._set_thread = None - st._finished(success=success) + + if _exception is not None: + st.set_exception(_exception) + else: + st.set_finished() + del th if self._set_thread is not None: diff --git a/ophyd/status.py b/ophyd/status.py index 689b5d0ac..bf8339cfe 100644 --- a/ophyd/status.py +++ b/ophyd/status.py @@ -592,9 +592,9 @@ def inner(status): # At least one is done. # If it failed, do not wait for the second one. if (not l_success) and l_done: - self._finished(success=False) + self.set_exception(self.left.exception()) elif (not r_success) and r_done: - self._finished(success=False) + self.set_exception(self.right.exception()) elif l_success and r_success and l_done and r_done: # Both are done, successfully.