Skip to content

Prometheus scrape calls IsIdle()/getCPUUsage() and shares sample state with the monitor loop: data race, and a scrape shrinks the idle window (false idle-stop possible) #952

Closed

Description

@scttfrdmn

Found while fixing #937. Verified by reading the code only; not observed on an instance and not reproduced under -race.

When observability.metrics.enabled is set, the Prometheus collector's Collect (pkg/observability/metrics/collector.go) runs on the HTTP server's goroutine and calls, on every scrape:

The monitor loop calls isIdle() on its own goroutine with the same fields and no lock.

Effects

  1. Data race on the sample fields (unsynchronised read-modify-write from two goroutines).
  2. The scrape steals the monitor loop's sample window. The idle thresholds are per sample, and are labelled per minute because the tick is one minute (100000 // 100KB/min). After a scrape, the loop's next delta covers only the time since the scrape. With a 15 s scrape interval, that can be as little as a few seconds, so ~200 KB/min of real traffic or disk I/O can read as under the threshold. That is the non-conservative direction: a false idle-stop. In practice the scrape's IsIdle usually returns at the CPU check, because it samples CPU twice within microseconds (zero jiffies, so unknown, spawn status reports CPU 0.0% on a 100%-busy instance: two different "unknown" sentinels rendered as measurements #734) and stops there. When a jiffy boundary falls between the two calls, it reaches the network and disk checks and steals those windows as well.
  3. Every scrape logs a Not idle: ... line, so the spored log interleaves scrape and tick decisions and is misleading to read.

Related, conservative-only: isIdle returns early (sessions, process, CPU), so the network and disk samples are only taken when the checks before them pass. The next delta after a busy stretch covers that whole stretch rather than a minute, and reads as active for one extra tick.

Suggested fix: sample all counters once per tick in the monitor loop, under a mutex. Have IsIdle()/status/metrics read the last decision and readings instead of taking new samples. Optionally, normalise deltas by elapsed time (bytes/min) so the threshold means what its comment says.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions