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

+33 -4
View File
@@ -33,6 +33,8 @@ _ERROR_IO_PENDING = 997
_ERROR_NO_MORE_ITEMS = 259
_WAIT_OBJECT_0 = 0
_WAIT_TIMEOUT = 0x102
# How long to wait for the driver to finish a cancelled IRP before giving up on the channel.
_ABANDON_GRACE_MS = 1000
class _GUID(ctypes.Structure):
@@ -193,6 +195,12 @@ class HidChannel:
)
self._wevent = _k32.CreateEventW(None, True, False, None)
self._revent = _k32.CreateEventW(None, True, False, None)
if not self._wevent or not self._revent:
raise _winerr("CreateEventW")
self._broken = False
# OVERLAPPED structs and buffers of IRPs we gave up on, kept alive so a late
# completion by the driver can never write into memory that has been reused.
self._abandoned: list = []
except Exception:
_k32.CloseHandle(self.handle)
raise
@@ -202,8 +210,26 @@ class HidChannel:
if h:
_k32.CloseHandle(h)
self.handle = None
# _abandoned is deliberately not cleared: closing the handle cancels the pending IRPs,
# but the driver may still complete them asynchronously. The channel object is dropped
# by the caller (a broken channel is always reopened), which frees the buffers then.
def _cancel(self, ov, buf, event, what: str) -> None:
"""Cancel a timed-out IRP and give the driver a moment to finish it.
If the device is wedged and the cancellation never completes, mark the channel broken:
further calls fail fast instead of queueing more IRPs behind a dead one, and the
caller's reconnect path closes the handle (which cancels the IRP for good).
"""
_k32.CancelIoEx(self.handle, ctypes.byref(ov))
if _k32.WaitForSingleObject(event, _ABANDON_GRACE_MS) == _WAIT_OBJECT_0:
return
self._broken = True
self._abandoned.append((ov, buf))
def send(self, payload: bytes, timeout_ms: int = 1000) -> None:
if self._broken:
raise HidError(f"HID write on {self.path}: channel abandoned after a timeout; reconnect it")
data = bytes(1) + payload
if len(data) < self.output_len:
data += bytes(self.output_len - len(data))
@@ -216,14 +242,15 @@ class HidChannel:
if ctypes.get_last_error() != _ERROR_IO_PENDING:
raise _winerr("WriteFile")
if _k32.WaitForSingleObject(self._wevent, timeout_ms) != _WAIT_OBJECT_0:
_k32.CancelIoEx(self.handle, ctypes.byref(ov))
_k32.GetOverlappedResult(self.handle, ctypes.byref(ov), ctypes.byref(written), True)
self._cancel(ov, buf, self._wevent, "write")
raise HidError(f"HID write timed out on {self.path}")
if not _k32.GetOverlappedResult(self.handle, ctypes.byref(ov), ctypes.byref(written), True):
raise _winerr("WriteFile (completion)")
def read(self, timeout_ms: int) -> bytes | None:
"""Read one input report, or None on timeout."""
if self._broken:
raise HidError(f"HID read on {self.path}: channel abandoned after a timeout; reconnect it")
buf = ctypes.create_string_buffer(self.input_len)
ov = _OVERLAPPED()
ov.hEvent = self._revent
@@ -232,8 +259,10 @@ class HidChannel:
if not _k32.ReadFile(self.handle, buf, self.input_len, None, ctypes.byref(ov)):
if ctypes.get_last_error() != _ERROR_IO_PENDING:
raise _winerr("ReadFile")
if _k32.WaitForSingleObject(self._revent, timeout_ms) == _WAIT_TIMEOUT:
_k32.CancelIoEx(self.handle, ctypes.byref(ov))
if _k32.WaitForSingleObject(self._revent, timeout_ms) != _WAIT_OBJECT_0:
self._cancel(ov, buf, self._revent, "read")
if self._broken:
raise HidError(f"HID read timed out on {self.path} and the device ignored the cancel")
ok = _k32.GetOverlappedResult(self.handle, ctypes.byref(ov), ctypes.byref(got), True)
return buf.raw[: got.value] if ok and got.value else None
if not _k32.GetOverlappedResult(self.handle, ctypes.byref(ov), ctypes.byref(got), True):