fix(panda): retry arming once after a forced reset instead of hanging in pre_scan
CI for csaxs_bec / test (push) Failing after 2m11s

PandaBoxOMNY inherited on_pre_scan, which waits forever for the PandA READY
event before sending Arm(). After rapid abort/retry cycles the readout thread
can be left parked, so READY never arrives and pre_scan_all_devices() hangs
with no error (flomni_fermat_scan 11998/12000).

on_pre_scan now runs a bounded arm task: wait pre_scan_timeout (default 5 s) for
READY; on failure force-reset the box, restart the readout thread (after the old
loop finished its cleanup) and try once more, printing and logging a warning with
diagnostics. If the second attempt fails too, the status fails with a
RuntimeError so the scan aborts. Arm() is sent from the task, not a status
callback, so the scan is only released after it went out. A stop is not retried.

Also records the outcome and the still-open ophyd_devices race in
AGENTS_mirko.md.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017szgTwuHG65YhjoBGiK2Gj
This commit is contained in:
x12saandClaude Sonnet 5 committed 2026-09-20 11:47:04 +02:00
1 parent bd85d5e673
commit bbede883b0
3 files changed
+222

No files matched your search

+90
View File
@@ -3,11 +3,14 @@
# pylint: skip-file
from __future__ import annotations
import threading
from unittest import mock
import numpy as np
import pytest
from ophyd import Staged
from ophyd_devices.devices.panda_box.panda_box import PandaState
from ophyd_devices.interfaces.base_classes.psi_device_base import DeviceStoppedError
from pandablocks.responses import FrameData
from csaxs_bec.devices.panda_box.panda_box import PandaBoxCSAXS
@@ -294,6 +297,93 @@ def test_panda_omny_complete_free_running_timeout(panda_omny_raw_stream):
mock_flush.assert_called_once()
def _deliver_ready_after(dev, delay: float) -> threading.Timer:
"""Simulate the PandA data thread delivering the READY event `delay` seconds from now."""
timer = threading.Timer(delay, dev._run_status_callbacks, args=(PandaState.READY,))
timer.daemon = True
timer.start()
return timer
def test_panda_omny_pre_scan_arms_when_ready(panda_omny, capsys):
dev = panda_omny
dev.pre_scan_timeout = 2
with (
mock.patch.object(dev, "_arm") as mock_arm,
mock.patch.object(dev, "_reset_panda") as mock_reset,
):
_deliver_ready_after(dev, 0.05)
status = dev.on_pre_scan()
status.wait(timeout=4)
assert status.success is True
mock_arm.assert_called_once()
mock_reset.assert_not_called()
assert "Forcing abort/reset" not in capsys.readouterr().out
def test_panda_omny_pre_scan_retries_once_then_succeeds(panda_omny, capsys):
"""No READY on the first attempt: the box is force-reset and arming retried. The retry is
printed and logged."""
dev = panda_omny
dev.pre_scan_timeout = 0.2
def _reset_then_ready():
_deliver_ready_after(dev, 0.05)
with (
mock.patch.object(dev, "_arm") as mock_arm,
mock.patch.object(dev, "_reset_panda", side_effect=_reset_then_ready) as mock_reset,
mock.patch("csaxs_bec.devices.panda_box.panda_box_omny.logger") as mock_logger,
):
status = dev.on_pre_scan()
status.wait(timeout=4)
assert status.success is True
mock_reset.assert_called_once()
mock_arm.assert_called_once()
assert any("Forcing abort/reset" in str(c) for c in mock_logger.warning.call_args_list)
assert any("armed successfully on attempt 2" in str(c) for c in mock_logger.info.call_args_list)
assert "Forcing abort/reset" in capsys.readouterr().out
def test_panda_omny_pre_scan_raises_after_retry_fails(panda_omny, capsys):
dev = panda_omny
dev.pre_scan_timeout = 0.1
with (
mock.patch.object(dev, "_arm") as mock_arm,
mock.patch.object(dev, "_reset_panda") as mock_reset,
mock.patch("csaxs_bec.devices.panda_box.panda_box_omny.logger") as mock_logger,
):
status = dev.on_pre_scan()
with pytest.raises(RuntimeError, match="could not be armed after 2 attempts"):
status.wait(timeout=4)
mock_arm.assert_not_called()
# once for the forced reset before the retry, once to leave the box clean at the end
assert mock_reset.call_count == 2
mock_logger.error.assert_called_once()
assert capsys.readouterr().out.count("Forcing abort/reset") == 1
# stale status callbacks of the failed attempts are removed again
assert dev._status_callbacks == {}
def test_panda_omny_pre_scan_stop_is_not_retried(panda_omny, capsys):
dev = panda_omny
dev.pre_scan_timeout = 0.3
with (
mock.patch.object(dev, "_arm") as mock_arm,
mock.patch.object(dev, "_reset_panda") as mock_reset,
):
status = dev.on_pre_scan()
threading.Event().wait(0.05)
dev._stop_stoppable_status_objects()
with pytest.raises(DeviceStoppedError):
status.wait(timeout=4)
# give a wrongly retrying task thread the time to show up
threading.Event().wait(0.5)
mock_arm.assert_not_called()
mock_reset.assert_not_called()
assert "Forcing abort/reset" not in capsys.readouterr().out
def test_panda_omny_raw_stream_signal_alias(panda_omny_raw_stream):
all_signal_names = [name for name, _ in panda_omny_raw_stream.data.signals]
assert "gate_detector_active" in all_signal_names