From 42cd7b1a78aa42e368b4f695dedb6b2ddeaf5f2a Mon Sep 17 00:00:00 2001 From: Christian Tabedzki <35670232+tabedzki@users.noreply.github.com> Date: Fri, 30 Oct 2026 10:33:11 -0400 Subject: [PATCH] fix: reject non-finite watchdog thresholds from the environment float() accepts "nan", "inf" and "-inf", so PIXELMAP_STALL_WARN_S and PIXELMAP_RSS_WARN_MB took them as thresholds. NaN breaks every stall comparison, and +inf (or inf/nan for the RSS limit) silently disables the check. Treat them like any other non-number: warn and use the default. Finite zero and negatives are still returned as-is, since they deliberately switch a check off. Assisted-by: ClaudeCode:claude-opus-5-5 --- pixelmap/gui/watchdog.py | 15 +++++++++++++-- tests/test_watchdog.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/pixelmap/gui/watchdog.py b/pixelmap/gui/watchdog.py index 616ba62..e1afe6a 100644 --- a/pixelmap/gui/watchdog.py +++ b/pixelmap/gui/watchdog.py @@ -29,6 +29,7 @@ from __future__ import annotations import logging +import math import os import sys import threading @@ -45,14 +46,24 @@ def _env_float(name: str, default: float) -> float: + """Read a threshold from the environment; blank or unset means ``default``. + + Finite zero and negatives are returned as-is (callers treat them as "off"). + ``nan`` and ``±inf`` parse as floats but aren't usable thresholds -- NaN + breaks every comparison, and infinity silently disables the check -- so they + are rejected like any other non-number. + """ raw = os.environ.get(name, "").strip() if not raw: return default try: - return float(raw) + value = float(raw) except ValueError: - log.warning("[watchdog] ignoring %s=%r: not a number, using %s", name, raw, default) + value = math.nan + if not math.isfinite(value): + log.warning("[watchdog] ignoring %s=%r: not a finite number, using %s", name, raw, default) return default + return value def _rss_mb() -> float: diff --git a/tests/test_watchdog.py b/tests/test_watchdog.py index 7212f82..d92e5c7 100644 --- a/tests/test_watchdog.py +++ b/tests/test_watchdog.py @@ -190,6 +190,37 @@ def test_env_float(monkeypatch, raw, expected): assert watchdog._env_float("PIXELMAP_TEST_KNOB", 7.0) == expected +@pytest.mark.parametrize( + "raw, expected", + # Finite zero and negatives stay accepted: they deliberately switch a check off. + [(" 12 ", 12.0), ("-1", -1.0), ("-0", 0.0), ("1e3", 1000.0)], +) +def test_env_float_accepts_finite_values(monkeypatch, raw, expected): + monkeypatch.setenv("PIXELMAP_TEST_KNOB", raw) + assert watchdog._env_float("PIXELMAP_TEST_KNOB", 7.0) == expected + + +@pytest.mark.parametrize("raw", ["nan", "NaN", "inf", "-inf", "Infinity", " inf "]) +def test_env_float_rejects_non_finite_values(monkeypatch, caplog, raw): + """``float()`` parses these, but none is a usable threshold: NaN makes every + stall comparison misbehave, and ±inf silently disables a check.""" + monkeypatch.setenv("PIXELMAP_TEST_KNOB", raw) + with caplog.at_level(logging.WARNING, logger=watchdog.log.name): + assert watchdog._env_float("PIXELMAP_TEST_KNOB", 7.0) == 7.0 + assert "PIXELMAP_TEST_KNOB" in caplog.text + + +@pytest.mark.parametrize("raw", [None, "", " "]) +def test_env_float_unset_or_blank_is_silent(monkeypatch, caplog, raw): + if raw is None: + monkeypatch.delenv("PIXELMAP_TEST_KNOB", raising=False) + else: + monkeypatch.setenv("PIXELMAP_TEST_KNOB", raw) + with caplog.at_level(logging.WARNING, logger=watchdog.log.name): + assert watchdog._env_float("PIXELMAP_TEST_KNOB", 7.0) == 7.0 + assert caplog.text == "" + + # -------------------------------------------------------------------------- # install() # --------------------------------------------------------------------------