Skip to content

Commit fcda9b5

Browse files
authored
Fixed race condition in producer Flush() operation (apache#229)
1 parent 23d4808 commit fcda9b5

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

pulsar/producer_partition.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ type pendingItem struct {
304304
batchData []byte
305305
sequenceID uint64
306306
sendRequests []interface{}
307+
completed bool
307308
}
308309

309310
func (p *partitionProducer) internalFlushCurrentBatch() {
@@ -329,6 +330,19 @@ func (p *partitionProducer) internalFlush(fr *flushRequest) {
329330
return
330331
}
331332

333+
// lock the pending request while adding requests
334+
// since the ReceivedSendReceipt func iterates over this list
335+
pi.Lock()
336+
defer pi.Unlock()
337+
338+
if pi.completed {
339+
// The last item in the queue has been completed while we were
340+
// looking at it. It's safe at this point to assume that every
341+
// message enqueued before Flush() was called are now persisted
342+
fr.waitGroup.Done()
343+
return
344+
}
345+
332346
sendReq := &sendRequest{
333347
msg: nil,
334348
callback: func(id MessageID, message *ProducerMessage, e error) {
@@ -337,11 +351,7 @@ func (p *partitionProducer) internalFlush(fr *flushRequest) {
337351
},
338352
}
339353

340-
// lock the pending request while adding requests
341-
// since the ReceivedSendReceipt func iterates over this list
342-
pi.Lock()
343354
pi.sendRequests = append(pi.sendRequests, sendReq)
344-
pi.Unlock()
345355
}
346356

347357
func (p *partitionProducer) Send(ctx context.Context, msg *ProducerMessage) (MessageID, error) {
@@ -422,6 +432,9 @@ func (p *partitionProducer) ReceivedSendReceipt(response *pb.CommandSendReceipt)
422432
sr.callback(msgID, sr.msg, nil)
423433
}
424434
}
435+
436+
// Mark this pending item as done
437+
pi.completed = true
425438
}
426439

427440
func (p *partitionProducer) internalClose(req *closeProducer) {

0 commit comments

Comments
 (0)