fix: collectors Close leaks, recover goroutine panics, validate sub-collectors (#2547)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
Jan-Otto Kröpke
2026-10-08 14:43:07 +02:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 48640e0070
commit 6e270a86a1
34 changed files with 1139 additions and 367 deletions
+75
View File
@@ -0,0 +1,75 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build windows
// Package recovery runs functions in a way that a panic is returned as an error
// instead of terminating the process. A panic in a goroutine cannot be recovered
// by the caller that started it, so every goroutine a collector starts must
// recover on its own.
package recovery
import (
"errors"
"fmt"
"runtime/debug"
"sync"
)
// ErrPanic is wrapped by every error that Run and Group return for a recovered panic.
var ErrPanic = errors.New("panic")
// Run calls fn and returns its error. If fn panics, Run recovers and returns
// an error wrapping [ErrPanic] that contains the panic value and the stack trace.
func Run(fn func() error) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%w: %v\n%s", ErrPanic, r, debug.Stack())
}
}()
return fn()
}
// Group runs functions in goroutines and collects all of their errors.
// A panic in one function is returned as an error by Wait and does not affect
// the other functions. The zero value is ready to use. Go may be called from
// within a function that runs in the group.
type Group struct {
wg sync.WaitGroup
mu sync.Mutex
errs []error
}
// Go calls fn through [Run] in a new goroutine.
func (g *Group) Go(fn func() error) {
g.wg.Go(func() {
if err := Run(fn); err != nil {
g.mu.Lock()
g.errs = append(g.errs, err)
g.mu.Unlock()
}
})
}
// Wait blocks until all functions have returned and returns their errors joined with [errors.Join].
func (g *Group) Wait() error {
g.wg.Wait()
g.mu.Lock()
defer g.mu.Unlock()
return errors.Join(g.errs...)
}
+92
View File
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build windows
package recovery_test
import (
"errors"
"sync/atomic"
"testing"
"github.com/prometheus-community/windows_exporter/internal/utils/recovery"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRun(t *testing.T) {
t.Parallel()
errExpected := errors.New("expected")
require.NoError(t, recovery.Run(func() error { return nil }))
require.ErrorIs(t, recovery.Run(func() error { return errExpected }), errExpected)
err := recovery.Run(func() error {
panic("boom")
})
require.ErrorIs(t, err, recovery.ErrPanic)
assert.Contains(t, err.Error(), "boom")
assert.Contains(t, err.Error(), "recovery_test.go", "error must contain the stack trace")
}
func TestGroup(t *testing.T) {
t.Parallel()
errExpected := errors.New("expected")
var (
g recovery.Group
done atomic.Int32
)
g.Go(func() error {
done.Add(1)
return nil
})
g.Go(func() error {
done.Add(1)
return errExpected
})
g.Go(func() error {
done.Add(1)
panic("boom")
})
g.Go(func() error {
// Nested Go calls must be waited for, too.
g.Go(func() error {
done.Add(1)
return nil
})
done.Add(1)
return nil
})
err := g.Wait()
require.ErrorIs(t, err, errExpected)
require.ErrorIs(t, err, recovery.ErrPanic)
assert.Contains(t, err.Error(), "boom")
assert.Equal(t, int32(5), done.Load())
var empty recovery.Group
require.NoError(t, empty.Wait())
}