hardwared: handle failed thermal readings without losing thermal state - #38529
Closed
JPL11 wants to merge 1 commit into
Closed
hardwared: handle failed thermal readings without losing thermal state#38529JPL11 wants to merge 1 commit into
JPL11 wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves thermal sensor fault-tolerance by propagating failed thermal reads as NaN, preventing NaN from poisoning hardwared’s thermal aggregation/filters, and adding an explicit “stale thermal readings” policy that blocks starting a drive when no valid readings exist for a timeout window.
Changes:
- Update
ThermalZone.read()to returnNaNon read/discovery failures (EIO/garbage/missing zone) instead of crashing or returning0. - Add
max_valid_temp()plusNaN-safe aggregation and a 10s “stale readings” gating policy inhardwared. - Add unit tests for ThermalZone read failures and
max_valid_temp()behavior.
Reviewed changes
Copilot reviewed 3 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| uv_sync.log | Adds a captured uv sync/build log output. |
| scons.log | Adds a captured scons build log output. |
| scons2.log | Adds a second captured scons build log output. |
| openpilot/common/hardware/base.py | Makes thermal zone reads return NaN on failures (rather than 0/crash). |
| openpilot/system/hardware/hardwared.py | Aggregates temps over valid readings only, avoids feeding NaN into filters, and adds stale-reading gating. |
| openpilot/system/hardware/tests/test_thermal_readings.py | Adds tests for the new failure semantics and NaN-safe max aggregation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+65
to
+69
| def max_valid_temp(temps) -> float: | ||
| # empty means the platform doesn't have these sensors; all-NaN means the | ||
| # reads are failing and the failure must propagate, not read as 0 | ||
| if len(temps) == 0: | ||
| return 0. |
|
|
||
| from openpilot.common.test import OpenpilotTestCase | ||
| from openpilot.common.hardware.base import ThermalZone | ||
| from openpilot.system.hardware.hardwared import max_valid_temp |
Comment on lines
+20
to
30
| if self.zone_number < 0: | ||
| for n in os.listdir("/sys/devices/virtual/thermal"): | ||
| if not n.startswith("thermal_zone"): | ||
| continue | ||
| with open(os.path.join("/sys/devices/virtual/thermal", n, "type")) as f: | ||
| if f.read().strip() == self.name: | ||
| self.zone_number = int(n.removeprefix("thermal_zone")) | ||
| break | ||
|
|
||
| with open(f"/sys/devices/virtual/thermal/thermal_zone{self.zone_number}/temp") as f: | ||
| return int(f.read()) / self.scale |
Comment on lines
+330
to
+334
| # must be at an engageable thermal band to go onroad; if we can't verify | ||
| # the device is cool (prolonged sensor failure), don't start a drive. | ||
| # while already onroad, stale readings hold the last band and log instead | ||
| # of forcing a disengagement over a sensor failure. | ||
| startup_conditions["device_temp_engageable"] = thermal_status < ThermalStatus.overheated and not thermal_readings_stale |
Contributor
Process replay diff reportReplays driving segments through this PR and compares the behavior to master. ✅ 0 changed, 66 passed, 0 errors |
JPL11
force-pushed
the
thermal-readings-policy
branch
from
August 4, 2026 17:05
9bacb93 to
325f332
Compare
Sensor reads can fail transiently (e.g. EIO when the spmi transaction fails), which currently crashes hardwared. Return NaN from ThermalZone.read on any read failure, aggregate over valid readings only (builtin max() with NaN is order-dependent, and NaN would poison the temperature filters permanently and freeze the thermal band), and track staleness: with no valid reading for 10s, block going onroad; while already onroad, hold the last band and log rather than force a disengagement over a sensor failure.
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #36690. Follow-up to #38424, which was closed asking the right question — what should the system do when there are no valid thermal readings. This PR implements a complete answer rather than just the crash fix.
Returning NaN alone turns out to be worse than the crash, because none of the consumers handle it:
max()with NaN is order-dependent (max(nan, 5)is nan,max(5, nan)is 5), so a failed sensor is sometimes masked, sometimes notFirstOrderFilter.update()poisons the filter state permanently — it never recovers even after the sensor doesall_comp_temp = nanboth band-transition comparisons are False, sothermal_statusfreezes at its last value andOFFROAD_DANGER_TEMPcan never tripSo:
ThermalZone.read()returns NaN on any read failure (the EIO in the issue, garbage values, missing zones) instead of crashing or returning a fake 0device_temp_engageablegoes False (if we can't verify the device is cool, don't start a drive — same conservative direction as the existingOFFROAD_DANGER_TEMPlogic). While already onroad, stale readings hold the last band and log an error rather than forcing a disengagement over a sensor failure. If you'd rather escalate tocriticalonroad (forcing offroad at the next opportunity), that's a one-line change — happy to flip it, this seemed like your call.Platforms without thermal sensors configured (PC) keep today's behavior: empty sensor lists still read as 0.0 and are never treated as stale.
Tests cover the EIO case from the issue, garbage reads, missing zones, the NaN-order-independence of the new aggregation, and that configured-but-failing sensors propagate instead of reading as 0.