diff --git a/src/openflexure_microscope_server/things/smart_scan.py b/src/openflexure_microscope_server/things/smart_scan.py index a6da0b51..5e065b32 100644 --- a/src/openflexure_microscope_server/things/smart_scan.py +++ b/src/openflexure_microscope_server/things/smart_scan.py @@ -351,10 +351,16 @@ class SmartScanThing(OFMThing): finally: # Save some final details but not set to the base model until after # resetting valued and unlocking just in case somehow pydantic errors. - if self._scan_data is not None: - final_image_count = self._scan_data.image_count - if self._preview_stitcher is not None: - last_timestamp = self.latest_preview_stitch_time + + final_image_count = ( + self._scan_data.image_count if self._scan_data is not None else None + ) + last_timestamp = ( + self.latest_preview_stitch_time + if self._preview_stitcher is not None + else None + ) + # However the scan finishes, unset all variables and release lock self._ongoing_scan = None self._scan_data = None @@ -364,8 +370,10 @@ class SmartScanThing(OFMThing): # Set the final details so they persist until the next scan if self._latest_scan_live_details is not None: - self._latest_scan_live_details.image_count = final_image_count - self._latest_scan_live_details.stitch_timestamp = last_timestamp + if final_image_count is not None: + self._latest_scan_live_details.image_count = final_image_count + if last_timestamp is not None: + self._latest_scan_live_details.stitch_timestamp = last_timestamp self._latest_scan_live_details.scan_phase = "Complete" # Remove any scan folders containing zero images. diff --git a/tests/unit_tests/test_smart_scan.py b/tests/unit_tests/test_smart_scan.py index 481091fb..545d23d1 100644 --- a/tests/unit_tests/test_smart_scan.py +++ b/tests/unit_tests/test_smart_scan.py @@ -559,7 +559,7 @@ def test_run_scan_cancelled(scan_thing_mocked_for_run_scan, caplog, mocker): scan_thing, "_main_scan_loop", side_effect=InvocationCancelledError() ) - result, logs, calls = check_run_scan(scan_thing, caplog) + result, logs, calls = check_run_scan(scan_thing, caplog, InvocationCancelledError) assert result == "cancelled by user" # No logs at warning level. @@ -571,7 +571,7 @@ def test_run_scan_cancelled(scan_thing_mocked_for_run_scan, caplog, mocker): "cam_change_streaming_mode_calls": 2, "main_scan_loop_calls": 1, "return_to_start_calls": 1, - "perform_final_stitch_calls": 1, + "perform_final_stitch_calls": 0, "save_scan_data_calls": 2, } assert calls == expected_calls_numbers