From 56d50441fe9adb8249a37d99a04d382c992b51f9 Mon Sep 17 00:00:00 2001 From: Lev Zakharov Date: Wed, 18 Oct 2023 15:35:13 +0300 Subject: [PATCH] fix: data race in Broker.AsyncProduce Signed-off-by: Lev Zakharov --- broker.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/broker.go b/broker.go index 71866ed66..268696cf4 100644 --- a/broker.go +++ b/broker.go @@ -435,12 +435,16 @@ type ProduceCallback func(*ProduceResponse, error) // // Make sure not to Close the broker in the callback as it will lead to a deadlock. func (b *Broker) AsyncProduce(request *ProduceRequest, cb ProduceCallback) error { - metricRegistry := b.metricRegistry + b.lock.Lock() + defer b.lock.Unlock() + needAcks := request.RequiredAcks != NoResponse // Use a nil promise when no acks is required var promise *responsePromise if needAcks { + metricRegistry := b.metricRegistry + // Create ProduceResponse early to provide the header version res := new(ProduceResponse) promise = &responsePromise{ @@ -466,8 +470,6 @@ func (b *Broker) AsyncProduce(request *ProduceRequest, cb ProduceCallback) error } } - b.lock.Lock() - defer b.lock.Unlock() return b.sendWithPromise(request, promise) }