Repository navigation
Description
Summary
loadCPUMetrics in cmd/metrics-v3-system-cpu.go reads the package-level resourceMetricsMap without taking resourceMetricsMapMu.RLock(), while other readers/writers of the same map (e.g. getResourceMetrics(), updateResourceMetrics()) correctly hold the mutex. This causes an unrecoverable fatal error: concurrent map read and map write when the metrics-v3 collector races with a concurrent writer.
Observed crash (production, MinIO RELEASE.2026-04-17T00-00-00Z, same code path still present on main)
fatal error: concurrent map read and map write
goroutine ... [running]:
internal/runtime/maps.fatal(...)
runtime/panic.go:1181 +0x18
github.com/minio/minio/cmd.loadCPUMetrics(...)
github.com/minio/minio/cmd/metrics-v3-system-cpu.go:72 +0x5a7
github.com/minio/minio/cmd.(*MetricsGroup).Collect(...)
github.com/minio/minio/cmd/metrics-v3-types.go:460 +0xc4
github.com/prometheus/client_golang/prometheus.(*Registry).Gather.func1()
github.com/prometheus/[email protected]/prometheus/registry.go:456 +0xfa
created by github.com/prometheus/client_golang/prometheus.(*Registry).Gather in goroutine 1291
github.com/prometheus/[email protected]/prometheus/registry.go:467 +0x58f
The process crashed and was restarted by systemd; no data loss, but it's an avoidable full-process crash triggered purely by concurrent metrics scraping (Prometheus registry gather racing with a background metrics writer).
Relevant code (current main)
cmd/metrics-v3-system-cpu.go, loadCPUMetrics:
cpuResourceMetrics, found := resourceMetricsMap[cpuSubsystem]
if found {
if cpuIdleMetric, ok := cpuResourceMetrics[getResourceKey(cpuIdle, nil)]; ok {
...
}
if cpuIOWaitMetric, ok := cpuResourceMetrics[getResourceKey(cpuIOWait, nil)]; ok {
...
}
}No resourceMetricsMapMu.RLock()/RUnlock() around this access.
Compare with cmd/metrics-resource.go, getResourceMetrics():
resourceMetricsMapMu.RLock()
defer resourceMetricsMapMu.RUnlock()
for _, subSys := range subSystems {
stats, found := resourceMetricsMap[subSys]
...
}and updateResourceMetrics():
resourceMetricsMapMu.Lock()
defer resourceMetricsMapMu.Unlock()
...loadCPUMetrics is the odd one out — it's a direct unsynchronized read of a map that is concurrently written elsewhere under resourceMetricsMapMu.Lock(), so it's a straightforward missed-lock bug, not an architectural issue.
Suggested fix
Wrap the resourceMetricsMap[cpuSubsystem] read (and the nested lookups) in loadCPUMetrics with resourceMetricsMapMu.RLock()/RUnlock(), same pattern as getResourceMetrics(). Worth grepping for other direct (non-get/update-wrapped) accesses to resourceMetricsMap in the metrics-v3-*.go files, in case the same miss exists elsewhere.
Environment
- Crash observed on:
RELEASE.2026-04-17T00-00-00Z(upstream minio/minio, now archived) - Code inspected against:
pgsty/silomainbranch, same function/lock pattern present, bug not fixed there either.
Activity
Vonng commentedon Sep 16, 2026
Thank you, @aschyolkin, for the detailed crash report and the precise diagnosis. We confirmed the missing lock and have implemented the fix in 027d43b, included in #214. The investigation and design decisions are recorded below.
Root cause and scope
updateResourceMetricsupdates both the outer subsystem map and the nestedResourceMetricsmap underresourceMetricsMapMu.Lock(). The v3 CPU loader read both maps without the corresponding read lock. A metrics scrape can therefore race with the background collector and terminate the server with a concurrent-map-access fatal error. The CPU cache has its own synchronization, which does not protect these resource maps.The unsynchronized reads were introduced by upstream f7b665347e, which added the v3 CPU metrics collector. We also checked all direct production accesses to
resourceMetricsMap: initialization, updates, and the existing resource-metrics reader already use the mutex; the v3 CPU loader was the missing reader.Chosen fix
We followed the existing locking contract and your suggested approach: acquire
resourceMetricsMapMu.RLock()immediately before the subsystem lookup, and deferRUnlock()until both idle and IOWait lookups are complete.The lock must cover both map levels. Fetching the nested map under a lock and then releasing it before reading its contents would still race, because the writer modifies that same nested map in place. Holding the existing read lock over the small lookup block avoids introducing a separate snapshot or map representation. CPU-cache loading remains outside this critical section.
The change preserves metric names, values, rounding, and existing omission of zero-valued metrics. It requires no configuration, API, or storage-format migration.
Regression coverage and validation
The retained tests exercise actual Prometheus
Registry.Gather()calls with one and four concurrent readers. They cover both CPU updates, which modify the nested map, and memory-subsystem updates, which also write the shared outer map. Writers remain active until the readers finish, and the tests check that writes made progress.Value tests cover absent or empty maps/subsystems, individual missing metrics, both resource metrics, rounding, and existing zero-value behavior. They validate exported metric names, gauge types, counts, and values. Fixtures use deterministic CPU data and restore the original global map after writers have stopped.
go test -race ./cmd -run '^TestLoadCPUMetrics' -count=10 -timeout=5mon Go 1.27.1, darwin/arm64.Thank you also to @mrjavadseydi for the correct patch and race regression test in #215. That PR was closed as superseded by the equivalent fix already in #214. Credit for the original report and diagnosis belongs to @aschyolkin; #215's patch contribution is acknowledged separately. Both contributors are listed in our contributor acknowledgements.
Status
PR #214 was merged into
mainon September 16, 2026, at 11:30 UTC as 5af0865, and this issue was automatically closed as completed. We verified thatmaincontains the fix commit027d43b. The validation above covers the implementation included in that PR; merge status is separate from publication of a fixed release.fix: synchronize CPU resource metrics reads