From ce441a0911018d96b2013378033c0f0be139ab64 Mon Sep 17 00:00:00 2001 From: serversdown Date: Thu, 29 Oct 2026 23:21:27 -0400 Subject: [PATCH] fix(sfm): a MiniMate start's read-back reconnects once when the modem dropped the line (#9) BE9441 over a modem, 2026-10-29: the start was acked, but the modem closed the line during the ~30 s sensor check, so the read-back's first wake hit '[Errno 32] Broken pipe' and the change was reported 503 'not checked' although the unit was monitoring. On an OSError in the read-back, a TCP unit is now reconnected once, its serial checked against the named unit, and its state read. Nothing is re-sent: the start still goes out exactly once. A serial cable is never reconnected (opening a MiniMate's port reboots it), a second failure or a silence (a cleanly closed line) ends 'not checked' as before, and a different unit after the reconnect is refused (409, change unconfirmed). Store consequence: none. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44 --- sfm/device_access/s3.py | 22 +++++++++++++- tests/device_access_fakes.py | 12 +++++++- tests/test_device_access_change.py | 49 ++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/sfm/device_access/s3.py b/sfm/device_access/s3.py index 92d88b2..fad91fb 100644 --- a/sfm/device_access/s3.py +++ b/sfm/device_access/s3.py @@ -227,10 +227,30 @@ class Series3Adapter: settle.append(S3_SENSOR_CHECK_S) # the first read waits out the check return {"subs": [f"0x{ack_sub:02X}"]} + reconnected: list = [] + def read_state() -> str: if settle: sleep(settle.pop()) - return _woken_state(client) + try: + return _woken_state(client) + except OSError as exc: + # The modem can drop the line while the unit sensor-checks + # after a start: BE9441's first wake hit "[Errno 32] Broken + # pipe" (2026-10-29), and a fresh connection read it as + # monitoring. Reconnect once, TCP only (opening a MiniMate's + # serial port reboots it), check it is still the same unit, + # and read. A read -- nothing is re-sent. + if reconnected or not address.host: + raise + reconnected.append(True) + log.warning("Series 3 at %s: the line dropped during the read-back (%s) -- " + "reconnecting to read the state; nothing is re-sent", + address.key, exc) + client.close() + client.open() + self._check(client.read_serial(), serial, address) + return _woken_state(client) return run_change( serial=serial, series=3, start=start, dry_run=dry_run, diff --git a/tests/device_access_fakes.py b/tests/device_access_fakes.py index cf656ae..1c0c79e 100644 --- a/tests/device_access_fakes.py +++ b/tests/device_access_fakes.py @@ -75,9 +75,13 @@ def default_s3_events(): class FakeMiniMateClient(_Base): def __init__(self, serial="BE18193", *, monitoring=False, compliance="default", events=None, - monitor_states=None, write_error=None, **kw): + monitor_states=None, write_error=None, serial_reported=None, wake_errors=None, **kw): super().__init__(**kw) self.serial = serial + # read_serial()'s answer (connect() keeps answering `serial`), and errors + # raised by wake() once a write has gone out -- the read-back's wakes. + self.serial_reported = serial if serial_reported is None else serial_reported + self.wake_errors = list(wake_errors or []) self.monitoring = monitoring self.monitor_states = list(monitor_states) if monitor_states else [monitoring] self.write_error = write_error @@ -107,6 +111,12 @@ class FakeMiniMateClient(_Base): def wake(self, **kw) -> None: self._step("wake") + if self.writes and self.wake_errors: + raise self.wake_errors.pop(0) + + def read_serial(self) -> str: + self._step("read_serial") + return self.serial_reported def get_monitor_status(self) -> MonitorStatus: self._step("get_monitor_status") diff --git a/tests/test_device_access_change.py b/tests/test_device_access_change.py index d4d4280..45cbe9a 100644 --- a/tests/test_device_access_change.py +++ b/tests/test_device_access_change.py @@ -797,3 +797,52 @@ def test_series3_a_start_waits_out_the_sensor_check_before_its_first_read(): da = access3(fake, sleep=slept.append) (da.start_monitoring if start else da.stop_monitoring)(TCP3, "BE18193") assert (S3_SENSOR_CHECK_S in slept) is waited + + +# ── #9: the read-back after a start survives a dropped modem line ───────────── +# BE9441 over a modem, 2026-10-29: the start was acked, and the read-back's first +# wake after the sensor check hit "[Errno 32] Broken pipe" -- the modem had closed +# the line. A fresh connection read the unit as monitoring a second later. + +SERIAL3 = UnitAddress(host=None, tcp_port=None, port="/dev/ttyUSB0", baud=38400) + + +def test_series3_start_read_back_reconnects_after_a_dropped_line(): + fake = FakeMiniMateClient(monitor_states=[False, True], + wake_errors=[BrokenPipeError(32, "Broken pipe")]) + r = access3(fake).start_monitoring(TCP3, "BE18193") + assert (r.outcome, r.verified, r.state_after) == ("sent", "ok", "monitoring") + assert fake.writes == ["start_monitoring"] + assert fake.opened == 2 and "read_serial" in fake.calls + + +def test_series3_read_back_reconnects_only_once(): + fake = FakeMiniMateClient(monitor_states=[False], + wake_errors=[BrokenPipeError(32, "x"), BrokenPipeError(32, "x")]) + with pytest.raises(ChangeUnconfirmed): + access3(fake).start_monitoring(TCP3, "BE18193") + assert fake.writes == ["start_monitoring"] and fake.opened == 2 + + +def test_series3_read_back_reconnect_refuses_a_different_unit(): + fake = FakeMiniMateClient(monitor_states=[False], wake_errors=[BrokenPipeError(32, "x")], + serial_reported="BE11529") + with pytest.raises(ChangeUnconfirmed) as e: + access3(fake).start_monitoring(TCP3, "BE18193") + assert e.value.http_status == 409 and fake.writes == ["start_monitoring"] + + +def test_series3_read_back_does_not_reconnect_on_a_serial_cable(): + # Opening a MiniMate's serial port reboots it (measured 2026-10-25). + fake = FakeMiniMateClient(monitor_states=[False], wake_errors=[BrokenPipeError(32, "x")]) + with pytest.raises(ChangeUnconfirmed): + access3(fake).start_monitoring(SERIAL3, "BE18193") + assert fake.opened == 1 and fake.writes == ["start_monitoring"] + + +def test_series3_read_back_silence_is_not_a_dropped_line(): + # A cleanly closed line reads as silence, not an OSError: no reconnect. + fake = FakeMiniMateClient(monitor_states=[False], wake_errors=[NoAnswer("quiet")]) + with pytest.raises(ChangeUnconfirmed): + access3(fake).start_monitoring(TCP3, "BE18193") + assert fake.opened == 1 and fake.writes == ["start_monitoring"]