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
This commit is contained in:
Julius Volz 2015-01-31 22:08:48 +01:00
parent e192dfc497
commit f3e101bd1c
1 changed files with 3 additions and 2 deletions

View File

@ -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) { 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{} var metricHashes map[uint64]struct{}
if r.collectChecksEnabled { if r.collectChecksEnabled {
metricHashes = make(map[uint64]struct{}) 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) metricChan := make(chan Metric, capMetricChan)
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
r.mtx.RLock()
metricFamiliesByName := make(map[string]*dto.MetricFamily, len(r.dimHashesByName))
// Scatter. // Scatter.
// (Collectors could be complex and slow, so we call them all at once.) // (Collectors could be complex and slow, so we call them all at once.)
r.mtx.RLock()
wg.Add(len(r.collectorsByID)) wg.Add(len(r.collectorsByID))
go func() { go func() {
wg.Wait() wg.Wait()