reconnect take 2
This commit is contained in:
1 parent
811fc7d66d
commit
f2aed8b8af
1 file changed
+5
-41
+5
-41
@@ -285,7 +285,6 @@ class TCPChamberReader:
|
||||
self._rx_count = 0
|
||||
self._err_count = 0
|
||||
self._backoff = self._BACKOFF_BASE
|
||||
self._conn_task: asyncio.Task | None = None
|
||||
|
||||
async def run(self) -> None:
|
||||
"""Immortal entry point — restarts _run_loop() after any unexpected crash."""
|
||||
@@ -307,15 +306,9 @@ class TCPChamberReader:
|
||||
self._running = True
|
||||
log.info(f"[TCP:{self.port}] Starting TCP chamber reader")
|
||||
|
||||
# Brief pause on startup to let firmware clean up any stale connections
|
||||
await asyncio.sleep(0.5)
|
||||
|
||||
while self._running:
|
||||
try:
|
||||
# Track connection task so stop() can cancel a blocking readline
|
||||
self._conn_task = asyncio.create_task(self._connect_and_read())
|
||||
await self._conn_task
|
||||
self._conn_task = None
|
||||
await self._connect_and_read()
|
||||
# Clean disconnect (firmware reboot / graceful close) — short pause
|
||||
self._backoff = self._BACKOFF_BASE
|
||||
if self._running:
|
||||
@@ -324,11 +317,10 @@ class TCPChamberReader:
|
||||
except asyncio.CancelledError:
|
||||
raise # propagate to run() so it exits cleanly
|
||||
except ConnectionRefusedError:
|
||||
# Bridge not ready yet (arduino-router restarting) — fast retry
|
||||
delay = 1.0 if self._backoff <= self._BACKOFF_BASE else 2.0
|
||||
log.info(f"[TCP:{self.port}] Connection refused — bridge restarting? retrying in {delay}s")
|
||||
# Bridge not ready yet (arduino-router restarting) — short fixed retry
|
||||
log.info(f"[TCP:{self.port}] Connection refused — bridge restarting? retrying in 3s")
|
||||
self._backoff = self._BACKOFF_BASE
|
||||
await asyncio.sleep(delay)
|
||||
await asyncio.sleep(3.0)
|
||||
except Exception as e:
|
||||
delay = min(self._backoff, self._BACKOFF_MAX)
|
||||
jitter = random.uniform(0, delay * 0.2)
|
||||
@@ -343,48 +335,20 @@ class TCPChamberReader:
|
||||
|
||||
async def stop(self) -> None:
|
||||
self._running = False
|
||||
if self._conn_task and not self._conn_task.done():
|
||||
self._conn_task.cancel()
|
||||
try:
|
||||
await self._conn_task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
log.info(f"[TCP:{self.port}] Connection task cancelled")
|
||||
|
||||
async def _connect_and_read(self) -> None:
|
||||
log.info(f"[TCP:{self.port}] Connecting to {self.host}:{self.port}")
|
||||
reader, writer = await asyncio.open_connection(self.host, self.port)
|
||||
log.info(f"[TCP:{self.port}] Connected")
|
||||
|
||||
# Track first payload aggressively to detect stale connections
|
||||
first_payload_received = False
|
||||
timeouts_since_data = 0
|
||||
|
||||
try:
|
||||
while self._running:
|
||||
try:
|
||||
# Shorter timeout for first payload to detect stale connections quickly
|
||||
timeout = 10.0 if not first_payload_received else 60.0
|
||||
raw = await asyncio.wait_for(reader.readline(), timeout=timeout)
|
||||
except asyncio.TimeoutError:
|
||||
if not first_payload_received:
|
||||
log.warning(f"[TCP:{self.port}] No initial payload within 10s — stale connection, reconnecting")
|
||||
return
|
||||
timeouts_since_data += 1
|
||||
if timeouts_since_data >= 2:
|
||||
log.warning(f"[TCP:{self.port}] No data for {60 * timeouts_since_data}s — connection stale, reconnecting")
|
||||
return
|
||||
log.debug(f"[TCP:{self.port}] Read timeout — waiting for next payload")
|
||||
continue
|
||||
|
||||
raw = await reader.readline()
|
||||
if not raw:
|
||||
log.warning(f"[TCP:{self.port}] Connection closed by MCU")
|
||||
return
|
||||
|
||||
line = raw.decode("utf-8", errors="replace").strip()
|
||||
if line:
|
||||
first_payload_received = True
|
||||
timeouts_since_data = 0
|
||||
await self._handle_line(line)
|
||||
finally:
|
||||
writer.close()
|
||||
|
||||
Reference in new issue
Block a user