Skip to content

Commit

Permalink
Merge pull request #2678 from lzakharov/fix-data-race-in-async-produce
Browse files Browse the repository at this point in the history
fix: data race in Broker.AsyncProduce
  • Loading branch information
prestona authored Nov 2, 2023
2 parents a15034a + 56d5044 commit f97ced2
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 f97ced2

Please sign in to comment.