Skip to content

power-guard: timing env overrides are never validated — CONFIRM=0/negative fires hybrid-sleep on the first critical sample, malformed/negative POLL crash-loops the daemon #6477

Closed

Description

@dezren39

Problem

nixos/services/power-guard/power-guard.py exists for one reason (issue #1687): UPower hybrid-slept amd-6 for 2h40m on a single glitched battery reading (2026-10-28), so the guard only fires the sleep itself after a sustained critical state — 300s at/below 5%, 60s at/below 2%.

Its three timing overrides are parsed at import with bare float() and never validated (power-guard.py:36, :41, :42):

POLL = float(os.environ.get("POWER_GUARD_POLL", "15"))
CONFIRM = float(os.environ.get("POWER_GUARD_CONFIRM", "300"))
EDGE_CONFIRM = float(os.environ.get("POWER_GUARD_EDGE_CONFIRM", "60"))

and consumed unchecked at :174/:176 (now - since >= CONFIRM / >= EDGE_CONFIRM) and :185 (time.sleep(POLL)). Consequences:

  1. POWER_GUARD_CONFIRM=0 (or any negative, or EDGE_CONFIRM=0) silently deletes the confirmation step. critical_since is set to now on the first critical sample and now - critical_since >= 0 is immediately true, so the guard fires hybrid-sleep on the first glitch sample — exactly the single-sample failure it was built to prevent.
  2. A malformed value (POWER_GUARD_POLL=abc) crashes the daemon at import with a raw ValueError traceback. The unit (nixos/services/power-guard/default.nix) runs Restart = "always", so this is a silent crash loop: the guard is simply absent, with no inhibitor held and no actionable log line naming the bad variable.
  3. POWER_GUARD_POLL=-1 crashes at the first time.sleep(POLL) (ValueError: sleep length must be non-negative); POLL=0 busy-spins the poll loop against sysfs.

The NixOS module sets none of these variables, so production runs the defaults today — this is a latent trap on a documented operator surface (the module docstring advertises them as "Threshold/timing overrides"), one typo'd drop-in or leftover test value away from either recreating the 2026-10-28 incident or disabling the guard outright.

Evidence (verified this run)

Probe on the laptop, script taken byte-identical from origin/main @252d430853, fake sysfs via the documented POWER_GUARD_SYSFS hook (BAT0: capacity 3, status Discharging; ACAD: online 0), POWER_GUARD_NO_INHIBIT=1, POWER_GUARD_SLEEP_CMD pointed at a marker file (probe dir ~/git/fleet/probe-power-guard):

  • A — POWER_GUARD_CONFIRM=0, POLL=0.2: log shows CRITICAL state entered (cap=3 tte=None); confirming followed in the same second by CONFIRMED: critical sustained 0s at cap=3 and the sleep command fired (marker written). The "sustained" requirement was 0 seconds.
  • E — POWER_GUARD_CONFIRM=-5: same — fires on the first sample.
  • B — control, POWER_GUARD_CONFIRM=300: CRITICAL state entered and no fire within the run window (marker absent). The confirmation logic itself works; only the override surface is unguarded.
  • C — POWER_GUARD_POLL=abc: ValueError: could not convert string to float: 'abc' at power-guard.py:36, process exits before the startup log line.
  • D — POWER_GUARD_POLL=-1: ValueError: sleep length must be non-negative at power-guard.py:185 (time.sleep(POLL)).

Dedup: power-guard issue search (open + closed) finds only #1687 (the original feature request this daemon implements) — nothing covers the override-validation gap. There is also no test file for the daemon anywhere in the repo (only power-guard.py, default.nix, and the host wiring reference it), despite the script shipping dedicated test hooks (POWER_GUARD_SYSFS, POWER_GUARD_NO_INHIBIT, POWER_GUARD_SLEEP_CMD).

Why it matters

This daemon is the safety net for the fleet's primary execution host. Its failure modes here are the worst kind: silent. A zero/negative confirm turns it back into the single-sample actor it replaced; a malformed value removes it entirely under a restarting unit that looks "active" from a distance. Fail-closed parsing (refuse to start with a named error, or clamp to safe minimums) is the house pattern for exactly this class (cf. the fleet-config validation issues).

Acceptance criteria

  • POWER_GUARD_POLL, POWER_GUARD_CONFIRM, and POWER_GUARD_EDGE_CONFIRM are parsed through one validating helper: non-numeric, NaN/inf, zero, and negative values are rejected with a single startup error naming the variable and the offending value (no raw traceback), OR clamped to documented safe minimums — triage picks one behaviour and applies it uniformly to all three.
  • With any rejected override the daemon exits non-zero before engaging in polling, so the failure is visible as a failed unit rather than a crash loop.
  • Valid overrides and the built-in defaults behave exactly as today (probe scenarios A–E above inverted: A/E can no longer fire on the first sample; B unchanged).
  • New tests exercise the parser and the fire/no-fire behaviour through the existing POWER_GUARD_SYSFS / POWER_GUARD_NO_INHIBIT / POWER_GUARD_SLEEP_CMD hooks (fake sysfs, stubbed sleep command), and are wired into a repo check (flake check or the scripts-test venue — triage's call).

Activity

  1. added
    kind/bugSomething isn't working (prefixed form of the classic bug label)
    task/triageQueue packet kind: verify, reproduce, and route this item
    on Nov 6, 2026
  2. developing-today commented on Nov 6, 2026

    @developing-today
    Contributor
  3. added
    source/creatorFiled by the new-issue-creator job
    priority/normalOrdinary priority (the default)
    area/nixosNixOS modules and machine configuration
    app/power-guardnixos/services/power-guard battery confirmation daemon
    on Nov 6, 2026
  4. dezren39 commented on Nov 6, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · filed

    • Agent: new-issue-creator (new-issue-creator-code-a)
    • Session / ID: cron new-issue-creator-code-a, run 2026-11-06 03:05 CST
    • Found via: filed this run
    • Task: Filed after verifying on the laptop (probe ~/git/fleet/probe-power-guard, real script from origin/main @252d430853, fake sysfs via POWER_GUARD_SYSFS): POWER_GUARD_CONFIRM=0 and =-5 fire hybrid-sleep on the first critical sample ("sustained 0s"); the 300s control holds; POWER_GUARD_POLL=abc crashes at import (power-guard.py:36) and POLL=-1 crashes at time.sleep (:185).
    • Expectation: effort s — triage routes the fix: one validating parser for POLL/CONFIRM/EDGE_CONFIRM (refuse-with-named-error or clamp, uniformly), plus the daemon's first tests through its existing fake-sysfs/stub-sleep hooks.
    • Links: probe dir ~/git/fleet/probe-power-guard @252d430853; body evidence in the issue.
  5. dezren39 commented on Nov 6, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · picked up

  6. added
    work/assignedQueue worker lifecycle state
    queue/claimedQueue worker lifecycle state
    and removed on Nov 6, 2026
  7. 36 remaining items

  8. dezren39 commented on Nov 6, 2026

    @dezren39
    MemberAuthor

    Parked — task 10951 (lane code-s) resumed PR #6485: Codex re-review of head 4ed01981b2 is complete with no findings, and both earlier Codex P1 findings plus both CodeRabbit findings are dispositioned (one CodeRabbit item tracked separately in issue #6502). The only remaining gate is CI: scripts-test run 42205146600 was still queued after a 5-minute poll. Task 10951 is parked for 1200s; on resume, merge PR #6485 if that run is green.

  9. dezren39 commented on Nov 6, 2026

    @dezren39
    MemberAuthor

    Resumed — task 10951 (lane code-s) re-verified PR #6485 fresh at 11:40Z: PR is OPEN, mergeable, head 4ed01981b2 unchanged; Codex review state on that head shows no findings and no unresolved findings, and both earlier Codex P1 findings plus both CodeRabbit findings carry disposition replies (one CodeRabbit item tracked separately in issue #6502). Issue #6477 is OPEN with no hard-stop labels. The only remaining gate is CI: scripts-test run 42205146600 (created 10:45Z) was still queued after another 5-minute poll. Task 10951 is parked for 1200s; on resume, merge PR #6485 if that run is green.

  10. added
    queue/claimedQueue worker lifecycle state
    and removed
    status/parkedDeliberately paused with a recorded resume condition
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    on Nov 6, 2026
  11. dezren39 commented on Nov 6, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · picked up

  12. added a commit that references this issue on Nov 6, 2026
  13. dezren39 commented on Nov 6, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    app/power-guardnixos/services/power-guard battery confirmation daemonarea/nixosNixOS modules and machine configurationkind/bugSomething isn't working (prefixed form of the classic bug label)priority/normalOrdinary priority (the default)source/creatorFiled by the new-issue-creator jobstatus:donetask/triageQueue packet kind: verify, reproduce, and route this item

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions