Skip to content

Commit

Permalink
fix: data race in Broker.AsyncProduce
Browse files Browse the repository at this point in the history
Signed-off-by: Lev Zakharov <[email protected]>
  • Loading branch information
lzakharov committed Nov 1, 2023
1 parent a15034a commit 56d5044
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
}

Expand Down

0 comments on commit 56d5044

Please sign in to comment.