From c25dc274e51d0e1a40a3cf043ad4280e33ed59d3 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 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/broker.go b/broker.go index 7ec3022b3f..a42500c754 100644 --- a/broker.go +++ b/broker.go @@ -434,7 +434,11 @@ 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 { + b.lock.Lock() + defer b.lock.Unlock() + metricRegistry := b.metricRegistry + needAcks := request.RequiredAcks != NoResponse // Use a nil promise when no acks is required var promise *responsePromise @@ -465,8 +469,6 @@ func (b *Broker) AsyncProduce(request *ProduceRequest, cb ProduceCallback) error } } - b.lock.Lock() - defer b.lock.Unlock() return b.sendWithPromise(request, promise) }