Fail safe, not dead: bound the HID cancel wait, isolate sensor failures, keep the fan worker alive

- hid.py: after a timed-out HID operation, wait at most 1 s for the driver to
  finish the cancelled IRP; if it does not, mark the channel broken so the
  caller's reconnect path closes it instead of blocking forever on a wedged
  device. Keep abandoned OVERLAPPED/buffer references alive until close so a
  late IRP completion cannot write into reused memory. Check CreateEventW.
- telemetry/service.py: read each source (CPU, GPU, system power, GPU memory)
  independently; a failing source now costs only its own readings instead of
  the whole sample, logged at most once a minute per source.
- sensors.py: the Commander PRO iCUE ownership check runs inside a guard; if
  ownership cannot be established the device is released rather than risking
  swapped replies (and the failure no longer escapes into the sample).
- cooling/control.py: an unexpected exception no longer ends fan control for
  the life of the process; fail safe, log at most once a minute, retry with a
  fresh sensor object after 5 s. failsafe() no longer raises, and control
  errors back off 5 s instead of re-opening the device every second.
This commit is contained in:
paul committed 2026-12-05 16:47:16 +13:00
1 parent 7356809c0a
commit d637d46777
5 files changed
+102 -26

No files matched your search

+23 -6
View File
@@ -222,7 +222,7 @@ class CaseFanGroup:
self._last_update = None
try:
self.commander.set_duty(self.ports, 100)
except (OSError, HidError):
except Exception: # the worker's finally block depends on this never raising
log.exception("Cannot set %s case fans to failsafe 100%%; controller unavailable", self.name)
@@ -262,6 +262,10 @@ class CpuTemperature:
class CaseFanControl:
# Pause after a control error before polling again, so a lost device is not re-opened
# (with a full traceback and process enumeration) every second.
ERROR_RETRY_SECONDS = 5.0
def __init__(self, commander: CommanderPro, gpu_ports: tuple[int, ...],
cpu_ports: tuple[int, ...] | None = None, top: TopFanCurve | None = None,
profile: FanProfile = BALANCED, sensors=None):
@@ -316,11 +320,15 @@ class CaseFanControl:
def run(self, stop: threading.Event) -> None:
gpu_factory, cpu_factory = self._sensors
cpu_temp = cpu_factory() if self.cpu is not None else None
gpu = None
reported: float | None = None # when the last unexpected error was logged
try:
gpu = gpu_factory()
while not stop.is_set():
now = time.monotonic()
retry_in = 1.0
try:
if gpu is None:
gpu = gpu_factory()
self.update(gpu.read().temp, now, cpu_temp.read(now) if cpu_temp else None)
except (OSError, HidError) as e:
self._error(f"CONTROL ERROR: {e}")
@@ -328,10 +336,19 @@ class CaseFanControl:
self.commander.close()
if self.top is not None:
self.top.update(100, None, time.monotonic())
stop.wait(1)
except Exception:
log.exception("Case fan worker failed")
self._error("CONTROL ERROR: fan worker stopped; check dashboard.log")
retry_in = self.ERROR_RETRY_SECONDS
except Exception as e:
# A sensor or control bug must not end fan control for the life of the
# process: fail safe, log at most once a minute, and retry with a freshly
# built sensor object.
if reported is None or now - reported >= 60:
reported = now
log.exception("Case fan control error; failing safe and retrying")
self._error(f"CONTROL ERROR: {e}")
self.failsafe()
gpu = None
retry_in = self.ERROR_RETRY_SECONDS
stop.wait(retry_in)
finally:
self.failsafe()
self.commander.close()