From db007176fd2d84246af7e0597839eca06923a243 Mon Sep 17 00:00:00 2001 From: henrygd Date: Sat, 31 Jan 2026 18:07:19 -0500 Subject: [PATCH] fix: prevent stale values in averaged stats due to json.Unmarshal reuse When reusing slices/structs with json.Unmarshal, fields marked with omitzero that are missing in the JSON are not reset to zero - they retain values from previous iterations. This caused containers without bandwidth data to inherit values from other containers that happened to occupy the same backing array position in previous records, resulting in inflated 10m averages. - Set containerStats to nil instead of [:0] to force fresh allocation - Reset tempStats each iteration in AverageSystemStats --- internal/records/records.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/records/records.go b/internal/records/records.go index 8d4b6580..b1deb88a 100644 --- a/internal/records/records.go +++ b/internal/records/records.go @@ -190,6 +190,8 @@ func (rm *RecordManager) AverageSystemStats(db dbx.Builder, records RecordIds) * id := record.Id // clear global statsRecord for reuse statsRecord.Stats = statsRecord.Stats[:0] + // reset tempStats each iteration to avoid omitzero fields retaining stale values + *stats = system.Stats{} queryParams["id"] = id db.NewQuery("SELECT stats FROM system_stats WHERE id = {:id}").Bind(queryParams).One(&statsRecord) @@ -444,9 +446,11 @@ func (rm *RecordManager) AverageContainerStats(db dbx.Builder, records RecordIds for i := range records { id := records[i].Id - // clear global statsRecord and containerStats for reuse + // clear global statsRecord for reuse statsRecord.Stats = statsRecord.Stats[:0] - containerStats = containerStats[:0] + // must set to nil (not [:0]) to avoid json.Unmarshal reusing backing array + // which causes omitzero fields to inherit stale values from previous iterations + containerStats = nil queryParams["id"] = id db.NewQuery("SELECT stats FROM container_stats WHERE id = {:id}").Bind(queryParams).One(&statsRecord)