From 0b1569510cac5d149fda024dcc173f4dd0dcfc7c Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Fri, 27 Jan 2023 00:03:46 +0100 Subject: [PATCH] fix(metrics): fix race condition when calling Broker.Open() twice (#2428) On failure, `Broker.Open()` can be called several times while a producer is running. In #2409, it was assumed that AsyncProduce can only be called with an open broker, however, it should be read that a user should call it after opening the broker. The broker could be disconnected and in progress of being reconnected. This is not hard to fix as we already have a lock protecting the creation of the registry: just don't create a new metric registry when attempting to reopen the broker. Fix #2320 (again) --- broker.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/broker.go b/broker.go index d049e9b47..231e65038 100644 --- a/broker.go +++ b/broker.go @@ -175,7 +175,9 @@ func (b *Broker) Open(conf *Config) error { b.lock.Lock() - b.metricRegistry = newCleanupRegistry(conf.MetricRegistry) + if b.metricRegistry == nil { + b.metricRegistry = newCleanupRegistry(conf.MetricRegistry) + } go withRecover(func() { defer func() {