Privilege split 4/11: the kurgan.hw service process

- kurgan/hw/service.py, HardwareService: one loop a second reads the CPU
  package (PawnIO MSR: temperature and power) and the GPU (NVML), runs the
  fan curves, and publishes a state snapshot. It never depends on a client:
  the pipe side takes no lock the loop needs, every sensor read is guarded
  (a fault is a missing temperature, so those fans fail safe), and a bug in
  the loop fails the fans safe and retries. On exit the case fans are left at
  100% and the top fans go back to the BIOS curve.
- Without saved ports it only reads the Commander PRO's RPMs (for the app's
  power estimate). With an invalid hw-settings.json it holds every connected
  PWM fan at 100% (CommanderPro.full_speed) and says why in its state,
  instead of refusing to start and leaving the fans at their last duty.
- State carries the app's cooling snapshot unchanged (profile, groups, top,
  note, row) plus the CPU temperature and power, GPU temperature, the
  Commander PRO RPMs and the RAM lighting. NaN never reaches the pipe.
- RAM lighting runs in the service; its chase follows the app's clock
  (SyncedChase, fed by ram_chase) so it stays in step with the other chases.
- kurgan/hw/settings.py: %ProgramData%\Kurgan\hw-settings.json, the cooling
  and RAM lighting sections, as strict as settings.json (validate_cooling is
  shared). The folder is created SYSTEM/Administrators full, Users read.
- kurgan/hw/main.py: serve() wires the loop, the RAM lighting and the pipe
  (the fans run whatever happens to the pipe); `python -m kurgan.hw` runs it
  in a console. The pipe user comes from HKLM\...\Services\KurganHw\
  Parameters\UserSid; the OpenRGB check reads that user's %APPDATA%.
- control.py: CaseFanControl.step() (one guarded pass) and shutdown()
  replace run(); CpuTemperature also keeps the latest reading, the package
  power and why it has none. The fan CLI saves to hw-settings.json.
- sensors.py: the Commander PRO mutex is Global\KurganCommanderPro, so a
  console tool in the user's session sees the service's claim.
- The app still uses the old worker; it is rewired in step 6.
- Tests: 16 new or rewritten (202 pass), including the loop with no client
  and a stuck client that can't stop fan control.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
paulandClaude Opus 5.5 committed 2026-12-05 19:28:32 +13:00
1 parent 73a424f6a8
commit 7190db733f
11 files changed
+971 -119

No files matched your search

+31 -39
View File
@@ -1,6 +1,5 @@
import argparse
import contextlib
import threading
import unittest
from unittest.mock import Mock, call, mock_open, patch
@@ -186,10 +185,7 @@ class TopFanCurveTest(unittest.TestCase):
control = CaseFanControl(commander, (4, 5, 6), None, self.top)
control.update(40, 0) # startup runs the case fans at 100%
self.assertTrue(self.top.boosted)
stop = threading.Event()
stop.set()
with patch("kurgan.cooling.control.GpuSensors"):
control.run(stop)
control.shutdown()
self.assertEqual(self.mask, 0)
self.board.close.assert_called()
@@ -228,12 +224,23 @@ class TopFanCurveTest(unittest.TestCase):
def test_cpu_temperature_is_averaged_and_gaps_are_not(self):
pkg = Mock()
pkg.temperature.side_effect = [50, 90, None, 60]
pkg.power.return_value = 35.0
cpu = CpuTemperature(factory=lambda: pkg)
self.assertEqual(cpu.read(0), 50)
self.assertEqual(cpu.read(1), 70)
self.assertEqual((cpu.temp, cpu.power, cpu.error), (90, 35.0, None)) # the latest, for the app
self.assertIsNone(cpu.read(2))
self.assertIsNotNone(cpu.error)
self.assertEqual(cpu.read(3), 60)
def test_cpu_temperature_failures_say_why_and_back_off(self):
cpu = CpuTemperature(factory=Mock(side_effect=OSError("Cannot open PawnIO")))
with self.assertLogs("kurgan.cooling.control", level="WARNING"):
self.assertIsNone(cpu.read(0))
self.assertEqual((cpu.temp, cpu.power, cpu.error), (None, None, "Cannot open PawnIO"))
cpu.read(30) # within the minute: not re-opened
self.assertEqual(cpu._factory.call_count, 1)
class Nct6687ControlTest(unittest.TestCase):
def test_override_uses_config_handshake_and_keeps_other_channels(self):
@@ -305,50 +312,35 @@ class FanWorkerTest(unittest.TestCase):
self.assertIn("FAILSAFE", self.control.status)
self.assertIn("[5]", self.control.status)
def test_exit_leaves_selected_ports_at_full_speed_even_without_lcd(self):
stop = threading.Event()
stop.set()
with patch("kurgan.cooling.control.GpuSensors"):
self.control.run(stop)
def test_shutdown_leaves_selected_ports_at_full_speed(self):
self.control.shutdown()
self.commander.set_duty.assert_called_once_with((4, 5, 6), 100)
self.commander.close.assert_called_once()
def test_worker_survives_unexpected_errors_and_keeps_failing_safe(self):
stop = threading.Event()
gpu = Mock()
reads = []
def read():
reads.append(1)
if len(reads) >= 3:
stop.set()
raise RuntimeError("sensor failure")
gpu.read.side_effect = read
with patch("kurgan.cooling.control.GpuSensors", return_value=gpu), \
patch.object(CaseFanControl, "ERROR_RETRY_SECONDS", 0):
with self.assertLogs("kurgan.cooling.control", level="ERROR"):
self.control.run(stop)
self.assertEqual(len(reads), 3) # retried instead of dying on the first error
def test_a_device_error_fails_safe_reopens_and_backs_off(self):
self.commander.set_duty.side_effect = [OSError("no reply to command 0x20"), {4: 900, 5: 900, 6: 900}]
self.assertEqual(self.control.step(50, None, 0), CaseFanControl.ERROR_RETRY_SECONDS)
self.assertIn("CONTROL ERROR", self.control.status)
self.commander.set_duty.assert_called_with((4, 5, 6), 100)
self.commander.close.assert_called_once() # reopened on the next pass
def test_a_control_bug_fails_safe_and_the_next_pass_runs(self):
ok = {p: 900 for p in range(1, 7)}
self.commander.set_duty.side_effect = [RuntimeError("bug"), ok, ok]
with self.assertLogs("kurgan.cooling.control", level="ERROR"):
self.assertEqual(self.control.step(50, None, 0), CaseFanControl.ERROR_RETRY_SECONDS)
self.assertIn("CONTROL ERROR", self.control.status)
self.assertEqual(self.commander.set_duty.call_args_list[1], call((4, 5, 6), 100))
self.assertEqual(self.control.step(50, None, 5), 1.0) # not dead: the next pass works
def test_unexpected_errors_fail_the_top_fans_safe_too(self):
top = Mock(spec=TopFanCurve)
self.control.top = top
stop = threading.Event()
gpu = Mock()
def read():
stop.set()
raise RuntimeError("sensor failure")
gpu.read.side_effect = read
with patch("kurgan.cooling.control.GpuSensors", return_value=gpu), \
patch.object(CaseFanControl, "ERROR_RETRY_SECONDS", 0):
with self.assertLogs("kurgan.cooling.control", level="ERROR"):
self.control.run(stop)
self.commander.set_duty.side_effect = [RuntimeError("bug"), {p: 900 for p in range(1, 7)}]
with self.assertLogs("kurgan.cooling.control", level="ERROR"):
self.control.step(50, None, 0)
self.assertEqual(top.update.call_args.args[:2], (100, None))
self.control.shutdown()
top.release.assert_called_once()