fix(sfm): any request taking the legacy path is refused beside a held line
The /device guard let every non-UM serial through before its held-line check. FastAPI drops an undeclared `serial`, so `?serial=BE18193` on a legacy route still ran Series-3 code at an address holding a Series-4 line -- the hole the guard exists to close. Serial-aware routes go straight to DeviceAccess, which refuses that case itself; everything else is checked. baud is no longer parsed (it is not in the address key, and pydantic's coercion made "38400.0" a second way past). Also, from the same review: - RingWatchTransport clears its frame state on every write, so a stray STX on an idle line cannot hide the ring that answers the next request. - Series4Session.is_open reads _client once; held_serial() calls it without the unit lock, and a concurrent hang-up between two reads was an AttributeError (a 500). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
1 parent
080e8f4704
commit
278921796a
6 files changed
+71
-11
No files matched your search
@@ -56,6 +56,9 @@ class RingWatchTransport(BaseTransport):
|
||||
return self._inner.is_connected
|
||||
|
||||
def write(self, data: bytes) -> None:
|
||||
# A request starts a new exchange (MicromateProtocol resets its parser
|
||||
# here too), so a stray STX on an idle line cannot hide the next ring.
|
||||
self._in_frame = self._escaped = False
|
||||
self._inner.write(data)
|
||||
|
||||
def read(self, n: int) -> bytes:
|
||||
@@ -121,7 +124,8 @@ class Series4Session:
|
||||
|
||||
@property
|
||||
def is_open(self) -> bool:
|
||||
return self._client is not None and self._client.is_open()
|
||||
client = self._client # one read: held_serial() calls this without the unit lock
|
||||
return client is not None and client.is_open()
|
||||
|
||||
def call_up(self):
|
||||
client = self._factory(self.address)
|
||||
|
||||
+8
-6
@@ -395,16 +395,18 @@ def _device_guard(path: str, query: Mapping[str, str]) -> Optional[tuple]:
|
||||
if not path.startswith("/device/") or path == "/device/hangup":
|
||||
return None
|
||||
serial = normalise_serial(query.get("serial") or "")
|
||||
if serial:
|
||||
if serial.startswith("UM") and path not in _SERIAL_AWARE_PATHS:
|
||||
return 501, f"not supported for Series 4 yet: {path}"
|
||||
return None
|
||||
if serial and path in _SERIAL_AWARE_PATHS:
|
||||
return None # DeviceAccess routes by series and checks held lines itself
|
||||
if serial.startswith("UM"):
|
||||
return 501, f"not supported for Series 4 yet: {path}"
|
||||
# Everything else runs the legacy Series-3 handler -- whatever serial it
|
||||
# carried, FastAPI drops it -- so it must not reach a held Series-4 line.
|
||||
if not (query.get("host") or query.get("port")):
|
||||
return None
|
||||
try:
|
||||
tcp_port = int(query.get("tcp_port") or DEFAULT_TCP_PORT)
|
||||
baud = int(query["baud"]) if query.get("baud") else None
|
||||
addr = _unit_address(query.get("port") or None, baud, query.get("host") or None, tcp_port)
|
||||
# baud is not part of the address key, so it is not parsed here.
|
||||
addr = _unit_address(query.get("port") or None, None, query.get("host") or None, tcp_port)
|
||||
except (BadRequest, ValueError):
|
||||
return None
|
||||
held = _device_access().held_serial(addr)
|
||||
|
||||
Reference in new issue
Block a user