mirror of
https://github.com/henrygd/beszel.git
synced 2026-08-12 21:37:46 +02:00
fix(agent): prevent a malformed Docker response from permanently breaking container stats (#2066)
decode() reused a single json.Decoder across responses. Once a decode errored on a truncated or malformed body (e.g. a container removed mid-request), the decoder stayed desynced and every subsequent decode failed, silently stopping all container stats until the agent was restarted. Decode the buffered bytes with json.Unmarshal instead, which keeps the reusable read buffer but holds no cross-call state.
Regression from d67d638.
This commit is contained in:
@@ -804,6 +804,24 @@ func TestGetDockerStatsRetriesVersionCheckUntilSuccess(t *testing.T) {
|
||||
assert.Equal(t, 2, requestCounts["/version"])
|
||||
}
|
||||
|
||||
// A failed decode must not break later decodes. Previously the reused json.Decoder
|
||||
// stayed desynced after one truncated response, breaking decode until restart.
|
||||
func TestDecodeRecoversFromError(t *testing.T) {
|
||||
dm := &dockerManager{}
|
||||
|
||||
// truncated JSON: body reads fine, decode fails
|
||||
var bad []container.ApiInfo
|
||||
err := dm.decode(&http.Response{Body: io.NopCloser(strings.NewReader(`[{"Id":"abc`))}, &bad)
|
||||
require.Error(t, err)
|
||||
|
||||
// the next decode must still succeed
|
||||
var good []container.ApiInfo
|
||||
err = dm.decode(&http.Response{Body: io.NopCloser(strings.NewReader(`[{"Id":"abcdef012345","Names":["/ok"]}]`))}, &good)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, good, 1)
|
||||
assert.Equal(t, "abcdef012345", good[0].Id)
|
||||
}
|
||||
|
||||
func TestCycleCpuDeltas(t *testing.T) {
|
||||
dm := &dockerManager{
|
||||
lastCpuContainer: map[uint16]map[string]uint64{
|
||||
|
||||
Reference in New Issue
Block a user