fix: temp sensor read failures could crash startup or permanently kill the task

Same bug class as the earlier heater fix: TempSensorTask.on_process()
called self.sensor.temperature() every tick with no exception handling
at all, and the constructor even called it once synchronously at
startup to prime the value. A read failure there would either crash
the whole server before the event loop started, or permanently kill
TempSensorTask's coroutine mid-run - nothing restarts a dead ATask, so
the sensor would never be read again for the rest of the process's
life.

TempSensor_max31865.temperature() now catches read failures and calls
a new reopen() (closes/reopens the spidev handle - the closest
equivalent to unplug/replug for a bus peripheral) instead of raising,
holding the last-known-good reading meanwhile. reopen() itself never
raises either, learned from HendiCtrl.disconnect() previously letting
a failure escape the same way. Belt-and-suspenders guards added at the
TempSensorTask level too, matching HeaterTask/StirrerTask.

Verified against a fake spidev: a read failure no longer raises,
reopen() is attempted automatically, and readings resume once the bus
responds again - even when reopen() itself also fails.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YaPLuRPpyjWcwhMvCvpHCL
This commit is contained in:
2026-07-03 21:45:21 +02:00
co-authored by Claude Sonnet 5
parent b6b79adff8
commit 4b9a144b62
2 changed files with 72 additions and 6 deletions
+16 -2
View File
@@ -16,7 +16,15 @@ class TempSensorTask(ATask):
# which would otherwise fire on_temp_changed() (and its
# asyncio.create_task()) before the event loop is running, since
# all tasks are built synchronously at module level in brewpi.py.
self.sensor.temperature()
# Guarded even though TempSensor_max31865.temperature() already
# catches its own read/reopen failures - belt-and-suspenders
# against a still-unanticipated exception crashing server startup
# entirely (same lesson as HeaterTask/StirrerTask's own outer
# guards - see tasks/heater.py's on_process()).
try:
self.sensor.temperature()
except Exception as e:
print(f"TempSensorTask: comm error priming sensor: {e}")
self.sensor.set_on_changed("temp", ChangedFloat(self.on_temp_changed, prec=1).set)
def on_temp_changed(self, value):
@@ -29,6 +37,12 @@ class TempSensorTask(ATask):
await self.msg_handler.send(data)
async def on_process(self):
# Guarded for the same reason as the priming call above - nothing
# restarts a dead ATask, so an exception escaping this loop would
# permanently stop this sensor from ever being read again.
while True:
self.sensor.temperature()
try:
self.sensor.temperature()
except Exception as e:
print(f"TempSensorTask: comm error reading sensor: {e}")
await asyncio.sleep(self.interval)