From 9c32af804167ba141cc331a6845dd0bd7463d030 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Thu, 26 Jan 2023 23:15:32 +0100 Subject: [PATCH] fix(metrics): fix race condition when calling Broker.Open() twice 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() {