Skip to content

Commit 8226aed

Browse files
authored
fix: crash during /reload from nil connection race condition (#971)
1 parent 40a2c35 commit 8226aed

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

query.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ func (q *Query) Collect(ctx context.Context, conn *sql.DB, ch chan<- Metric) {
125125

126126
// run executes the query on the provided database, in the provided context.
127127
func (q *Query) run(ctx context.Context, conn *sql.DB) (*sql.Rows, errors.WithContext) {
128+
if conn == nil {
129+
return nil, errors.Errorf(q.logContext, "nil database connection")
130+
}
131+
128132
if slog.Default().Enabled(ctx, slog.LevelDebug) {
129133
start := time.Now()
130134
defer func() {

target.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,13 @@ func (t *target) Collect(ctx context.Context, ch chan<- Metric) {
136136
var wg sync.WaitGroup
137137
// Don't bother with the collectors if target is down.
138138
if targetUp {
139+
conn := t.conn
139140
wg.Add(len(t.collectors))
140141
for _, c := range t.collectors {
141142
// If using a single DB connection, collectors will likely run sequentially anyway. But we might have more.
142143
go func(collector Collector) {
143144
defer wg.Done()
144-
collector.Collect(ctx, t.conn, ch)
145+
collector.Collect(ctx, conn, ch)
145146
}(c)
146147
}
147148
}

0 commit comments

Comments
 (0)