Repository navigation
Conversation
This was referenced Oct 15, 2026
scttfrdmn
force-pushed
the
fix/952
branch
3 times, most recently
from
October 17, 2026 01:53
d4969c9 to
68b7d45
Compare
…snapshot (#952) The Prometheus collector's Collect runs on the HTTP goroutine and called IsIdle() and getCPUUsage() on every scrape, advancing the same prev* sample fields as the monitor loop with no lock. Besides the data race, the scrape moved the baseline: the loop's next delta covered only the seconds since the scrape, so traffic over the per-tick threshold could read as idle. - sampleIdleSignals reads CPU, network and disk together, every tick, before any check can return (the early returns used to leave later counters unsampled, so the next delta spanned several ticks). - idleDecision decides from those readings plus the stateless signals and returns the log line instead of printing it; evaluateIdle samples, decides and publishes an idleSnapshot under idleMu. - checkAndAct evaluates every tick, idle timeout or not, and logs the reason only when an idle timeout is set. - IsIdle, GetCPUUsageMeasured, GetNetworkBytesMeasured, GetDiskIOBytesMeasured and the metrics CPU reader return the snapshot and take no sample. Before the first tick they report unknown ("no sample yet"), and the metrics CPU reader returns an error rather than 100%. - lastActivityTime is read and written under idleMu (the scrape and the spot webhooks read it off-loop); GetConfig reads through cfg(). - spored status owns its own Agent, so it calls the new EvaluateIdle once and reads everything from that one evaluation. - Removed the unused GetCPUUsage/GetNetworkBytes/getCPUUsage. (#952)
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #952. Stacked on #960 (fix/953), which is stacked on #955 → #936. The base is
fix/953, so the diff shows only #952.What was wrong
With
observability.metrics.enabled,Collector.Collectruns on the HTTP goroutine and calledIsIdle()andgetCPUUsage()on every scrape. Those advanceprevCPU*,prevNet*/netSampledandprevDisk*/diskSampled, the same fields the monitor loop uses, with no lock. That caused:Not idle: ...log line on every scrape.Separately,
isIdlereturned early (sessions, process, busy CPU) before sampling the network and disk, so the next delta after a busy stretch covered several ticks.What changed
sampleIdleSignalsreads CPU, network and disk together on every tick, before any check can return.idleDecision(readings)makes the decision from those readings plus the stateless signals (DCV, sessions, processes, GPU). It returns the log line rather than printing it.evaluateIdlesamples, decides and publishes anidleSnapshotunder a newidleMu. Only the owner of sampling calls it. In the daemon that is the monitor loop (checkAndActstep 4, every tick, whether or not an idle timeout is set; the reason is logged only when one is). In the one-shotspored status, which owns its own Agent, it is the new exportedEvaluateIdle().IsIdle,GetCPUUsageMeasured,GetNetworkBytesMeasured,GetDiskIOBytesMeasuredand the metricsGetCPUUsageContextall read the snapshot. Before the first tick they report unknown ("no sample yet", assume-active values), and the metrics CPU reader returns an error, so no sample is exported in place of the assumed 100%.lastActivityTimeis read and written underidleMu. The scrape (GetLastActivityTime) and the spot-interruption webhook payloads read it off-loop.GetConfignow reads throughcfg(), because the scrape calls it while the loop swaps the config.GetCPUUsage,GetNetworkBytesandgetCPUUsage.pkg/agentis not part of the public surface.I did not normalise deltas by elapsed time. With the loop as the only sampler, a window is always at least one ticker interval, and a delayed tick only makes the window longer, which errs towards active.
Tests (
pkg/agent/idle_snapshot_test.go)TestScrapeDoesNotShortenTheMonitorLoopWindow: baseline tick, then 150 KB, then a realmetrics.NewCollector(a).Collectscrape, then 50 KB, then a tick. The tick must measure all 200 KB and decide not idle.TestScrapeTakesNoSample: scrapes and all the readers take zero counter reads.TestEveryTickSamplesEveryCounter: a busy-CPU tick with a 10 MB transfer, then a quiet tick, which must measure only its own 5000/4096 bytes.TestReadersBeforeTheFirstTickAreUnknown.TestMonitorTicksAndScrapesDoNotRace: loop ticks concurrently with scrapes and off-loop readers. It is meaningful under-race, which CI already runs on./pkg/agent/....Revert checks. Each revert compiled (
go vetclean), and the test ran and failed:IsIdle()evaluates and the metrics CPU reader samples (the old behaviour)ScrapeDoesNotShortenTheMonitorLoopWindow("measured 50000 network bytes (measured=true), want all 200000"),ScrapeTakesNoSample,ReadersBeforeTheFirstTickAreUnknown,MonitorTicksAndScrapesDoNotRace(43DATA RACEreports under-race)EveryTickSamplesEveryCounter,ScrapeDoesNotShortenTheMonitorLoopWindowgo test -race ./pkg/agentpasses. The wholepkg/agentsuite also passes on Linux: aGOOS=linux GOARCH=arm64test binary run in an AL2023 container.make checkpasses except for the known Docker-onlyTestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker.make check-nestedpasses: all 9 modules are clean.Caveats
metrics.Collector, not through a live HTTP server.spawn_idle_statenow reports the last tick's decision, up to a minute old, rather than a fresh evaluation, and it is 0 until the first tick.nvidia-smicall per minute on GPU hosts that set no idle timeout. Before, only a scrape did that.