Skip to content

Commit aa1328d

Browse files
committed
Merge pull request #62 from prometheus/fix-writepb-race
Fix race condition in writePB().
2 parents e192dfc + f3e101b commit aa1328d

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

prometheus/registry.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,17 +400,18 @@ func (r *registry) ServeHTTP(w http.ResponseWriter, req *http.Request) {
400400
}
401401

402402
func (r *registry) writePB(w io.Writer, writeEncoded encoder) (int, error) {
403-
metricFamiliesByName := make(map[string]*dto.MetricFamily, len(r.dimHashesByName))
404403
var metricHashes map[uint64]struct{}
405404
if r.collectChecksEnabled {
406405
metricHashes = make(map[uint64]struct{})
407406
}
408407
metricChan := make(chan Metric, capMetricChan)
409408
wg := sync.WaitGroup{}
410409

410+
r.mtx.RLock()
411+
metricFamiliesByName := make(map[string]*dto.MetricFamily, len(r.dimHashesByName))
412+
411413
// Scatter.
412414
// (Collectors could be complex and slow, so we call them all at once.)
413-
r.mtx.RLock()
414415
wg.Add(len(r.collectorsByID))
415416
go func() {
416417
wg.Wait()

0 commit comments

Comments
 (0)