From 73b60890123b0cbe505e5d385224be1af293559e Mon Sep 17 00:00:00 2001 From: serversdown Date: Wed, 21 Oct 2026 01:23:57 -0400 Subject: [PATCH] fix(sfm): retry once on a dropped held line; refuse Series-3 beside a held Series-4 line Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44 --- sfm/device_access/access.py | 35 +++++++++++++++++++++++++++------ tests/test_device_access.py | 39 ++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 7 deletions(-) diff --git a/sfm/device_access/access.py b/sfm/device_access/access.py index a7c95eb..aaaf858 100644 --- a/sfm/device_access/access.py +++ b/sfm/device_access/access.py @@ -9,7 +9,7 @@ import threading import time from typing import Any, Callable, Dict, Optional -from .errors import DeviceError +from .errors import ConnectFailed, DeviceError, NoAnswer, WrongUnit from .locks import UnitLocks from .models import UnitAddress, UnitInfo, UnitSettings, UnitStatus from .s3 import Series3Adapter, default_s3_client_factory @@ -75,17 +75,40 @@ class DeviceAccess: series = series_for_serial(serial) # raises BadRequest before any I/O with self._locks.hold(address.key): if series == 3: + with self._sessions_guard: + held = self._sessions.get(address.key) + if held is not None and held.is_open: + raise WrongUnit( + f"a Series-4 line to {held.serial} is held at {address.key}; " + "a Series-3 unit cannot be there -- POST /device/hangup first " + "if the modem has moved") return s3_op(address, serial) session = self._session_for(address, serial) + was_held = session.is_open try: return session.run(lambda client: s4_op(client, serial)) - except DeviceError: - if not session.is_open: - with self._sessions_guard: - if self._sessions.get(address.key) is session: - del self._sessions[address.key] + except DeviceError as exc: + self._forget_if_closed(address, session) + # A held line the peer dropped still reads open (the transport only + # learns at EOF), so the first request on it times out or fails. + # Call up fresh and retry ONCE. Safe only because phase 1a + # operations are reads (idempotent); writes (phase 3) must NOT + # inherit this -- a write whose ack was lost must never be sent twice. + if was_held and isinstance(exc, (NoAnswer, ConnectFailed)): + self._drop(address.key, session) + session.hang_up() + session = self._session_for(address, serial) + try: + return session.run(lambda client: s4_op(client, serial)) + except DeviceError: + self._forget_if_closed(address, session) + raise raise + def _forget_if_closed(self, address: UnitAddress, session: Series4Session) -> None: + if not session.is_open: + self._drop(address.key, session) + def _session_for(self, address: UnitAddress, serial: str) -> Series4Session: with self._sessions_guard: session = self._sessions.get(address.key) diff --git a/tests/test_device_access.py b/tests/test_device_access.py index c11e9dc..8105c09 100644 --- a/tests/test_device_access.py +++ b/tests/test_device_access.py @@ -13,7 +13,10 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from device_access_fakes import FakeMiniMateClient, FakeMicromateClient # noqa: E402 from sfm.device_access.access import DeviceAccess # noqa: E402 -from sfm.device_access.errors import BadRequest, CallHomeInProgress, UnitBusy # noqa: E402 +import micromate.protocol as mm_proto # noqa: E402 +from sfm.device_access.errors import ( # noqa: E402 + BadRequest, CallHomeInProgress, ConnectFailed, UnitBusy, WrongUnit, +) from sfm.device_access.models import UnitAddress # noqa: E402 from sfm.device_access.s4 import RingDetected # noqa: E402 @@ -115,6 +118,40 @@ def test_hang_up_closes_a_held_line(): assert da.hang_up(ADDR) is False # nothing left to hang up +def test_a_peer_dropped_line_that_still_reads_open_is_retried_once(): + first, second = FakeMicromateClient(), FakeMicromateClient() + da = access(s4=[first, second]) + da.status(ADDR, "UM20147") + first.fail = [mm_proto.TimeoutError("quiet")] # _open untouched: looks open + st = da.status(ADDR, "UM20147") + assert st.series == 4 and first.closed == 1 and second.opened == 1 + + +def test_a_failed_fresh_call_up_is_not_retried(): + da = access(s4=[FakeMicromateClient(open_error=ConnectionRefusedError("no"))]) + with pytest.raises(ConnectFailed): + da.status(ADDR, "UM20147") # a 2nd attempt would pop an empty queue + + +def test_a_ring_on_a_held_line_is_not_retried(): + first, second = FakeMicromateClient(), FakeMicromateClient() + da = access(s4=[first, second]) + da.status(ADDR, "UM20147") + first.fail = [RingDetected("RADIO RING")] + with pytest.raises(CallHomeInProgress): + da.status(ADDR, "UM20147") + assert second.opened == 0 + + +def test_series3_beside_a_held_series4_line_is_refused_without_io(): + micro, mini = FakeMicromateClient(), FakeMiniMateClient() + da = access(s3=[mini], s4=[micro]) + da.status(ADDR, "UM20147") + with pytest.raises(WrongUnit): + da.status(ADDR, "BE18193") + assert mini.opened == 0 and micro.closed == 0 and micro.is_open() + + # ── the reaper ──────────────────────────────────────────────────────────────── def test_tick_listens_then_keeps_alive_then_hangs_up_when_idle():