From f3e101bd1c009ac71fce5cce1351fc1514c365a2 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Sat, 31 Jan 2015 22:08:48 +0100 Subject: [PATCH] Fix race condition in writePB(). The RLock already needs to be acquired when reading r.dimHashesByName. This fixes https://github.com/prometheus/client_golang/issues/61 --- prometheus/registry.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/prometheus/registry.go b/prometheus/registry.go index 551fb92..9959b3d 100644 --- a/prometheus/registry.go +++ b/prometheus/registry.go @@ -400,7 +400,6 @@ func (r *registry) ServeHTTP(w http.ResponseWriter, req *http.Request) { } func (r *registry) writePB(w io.Writer, writeEncoded encoder) (int, error) { - metricFamiliesByName := make(map[string]*dto.MetricFamily, len(r.dimHashesByName)) var metricHashes map[uint64]struct{} if r.collectChecksEnabled { metricHashes = make(map[uint64]struct{}) @@ -408,9 +407,11 @@ func (r *registry) writePB(w io.Writer, writeEncoded encoder) (int, error) { metricChan := make(chan Metric, capMetricChan) wg := sync.WaitGroup{} + r.mtx.RLock() + metricFamiliesByName := make(map[string]*dto.MetricFamily, len(r.dimHashesByName)) + // Scatter. // (Collectors could be complex and slow, so we call them all at once.) - r.mtx.RLock() wg.Add(len(r.collectorsByID)) go func() { wg.Wait()