Skip to content

spawn status reports CPU 0.0% on a 100%-busy instance: two different "unknown" sentinels rendered as measurements #734

Closed

Description

@scttfrdmn

Reported from the 2026-10-07 demo: the same spawn status output contained

Not idle: CPU usage 100.00% >= 20.00%
...
  CPU:              0.0%

Neither number is a measurement. Both are sentinels, they come from two
consecutive calls to a stateful function, and they disagree because they are
different sentinels.

The mechanism

getCPUUsage (pkg/agent/agent.go:1199) is stateful and destructive — it
overwrites the stored counters on every call:

prevIdle, prevTotal := a.prevCPUIdle, a.prevCPUTotal
a.prevCPUIdle, a.prevCPUTotal = idle, total

if prevTotal == 0 {
    return 100.0            // (A) first call, no delta: "assume active"
}
deltaTotal := total - prevTotal
if deltaTotal == 0 {
    return 0.0              // (B) no time elapsed: ???
}

buildStatusReport calls it twice, microseconds apart, in one fresh process:

  • cmd/spored/main.go:465 — isIdle := ag.IsIdle() → isIdle() →
    getCPUUsage(). prevTotal == 0, so branch (A) returns 100.0 and
    agent.go:1151 logs Not idle: CPU usage 100.00%.
  • cmd/spored/main.go:526 — CPUPercent: ag.GetCPUUsage(). Jiffies advance at
    ~100 Hz; microseconds later the delta is 0, so branch (B) returns 0.0,
    which is printed as CPU: 0.0%.

Every spawn status is a new spored status process, so prevTotal is always 0
on entry and this happens every time — the reported pair of values is the
normal output, not a race that was unlucky once.

The part that matters beyond the display

Branch (B) returns 0.0 from a no-information state. The two sibling unknown
paths in the same function both return 100.0 ("assume active"), deliberately,
and so does getNetworkBytes (return 1000000 on an unreadable counter). (B) is
the only unknown path that returns the most reassuring possible value, and
isIdle() consumes it — so the one inconsistent sentinel is the one that feeds
the auto-shutdown decision.

In the daemon the ticker spaces calls ≥1s apart, so deltaTotal > 0 and the idle
path is not currently reachable through (B). That makes this a latent hazard
rather than a live one, which is a reason to fix it while it is cheap.

Suggested fix

  1. Make (B) consistent: a zero delta is "unknown", so return 100.0 like
    every other unknown path in the file — or better, change the signature to
    (float64, bool) / return an error so a caller can tell "0% busy" from "could
    not tell", since those must not print identically.
  2. Sample once per report. buildStatusReport should read CPU once and pass
    that value to both the idle decision and the report, rather than calling a
    destructive function twice and printing both results. That alone removes the
    contradiction regardless of (1).
  3. For a one-shot process, a single-sample delta is meaningless anyway. Either
    take two samples ~200 ms apart inside status, or report
    CPU: unknown (single sample) — an honest unknown beats a confident 0.0% on
    a box running at 100%.

Verification note

Confirmed by code path plus the field observation above, which matches the
predicted pair of values exactly. Not reproduced locally: sysReadCPUTimes is
build-tagged and the darwin implementation (pkg/agent/sys_other.go:16) always
returns errBadProcStat, so both calls take the error branch and return 100.0
on a macOS controller. A regression test for this needs //go:build linux, or
sysReadCPUTimes promoted to a package var so it can be stubbed — the latter is
probably worth doing as part of the fix, since the delta logic is currently
untestable on the platform most development happens on.

Environment: spawn v0.123.0, spored v0.124.0, Amazon Linux 2023 instance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions