Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid memory access on the first pending batch receive callback #441

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions lib/ConsumerImplBase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ void ConsumerImplBase::doBatchReceiveTimeTask() {
long diff =
batchReceivePolicy_.getTimeoutMs() - (TimeUtils::currentTimeMillis() - batchReceive.createAt_);
if (diff <= 0) {
Lock batchOptionLock(batchReceiveOptionMutex_);
notifyBatchPendingReceivedCallback(batchReceive.batchReceiveCallback_);
batchOptionLock.unlock();
batchPendingReceives_.pop();
notifyBatchPendingReceivedCallback(popBatchReceiveCallback());
} else {
hasPendingReceives = true;
timeToWaitMs = diff;
Expand All @@ -96,20 +93,17 @@ void ConsumerImplBase::doBatchReceiveTimeTask() {
void ConsumerImplBase::failPendingBatchReceiveCallback() {
Lock lock(batchPendingReceiveMutex_);
while (!batchPendingReceives_.empty()) {
OpBatchReceive opBatchReceive = batchPendingReceives_.front();
batchPendingReceives_.pop();
listenerExecutor_->postWork(
[opBatchReceive]() { opBatchReceive.batchReceiveCallback_(ResultAlreadyClosed, {}); });
auto callback = popBatchReceiveCallback();
listenerExecutor_->postWork([callback]() { callback(ResultAlreadyClosed, {}); });
}
}

void ConsumerImplBase::notifyBatchPendingReceivedCallback() {
Lock lock(batchPendingReceiveMutex_);
if (!batchPendingReceives_.empty()) {
OpBatchReceive& batchReceive = batchPendingReceives_.front();
batchPendingReceives_.pop();
auto callback = popBatchReceiveCallback();
lock.unlock();
notifyBatchPendingReceivedCallback(batchReceive.batchReceiveCallback_);
notifyBatchPendingReceivedCallback(callback);
}
}

Expand Down
8 changes: 8 additions & 0 deletions lib/ConsumerImplBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ class ConsumerImplBase : public HandlerBase {

virtual void setNegativeAcknowledgeEnabledForTesting(bool enabled) = 0;

// Note: it should be protected by batchPendingReceiveMutex_ and called when `batchPendingReceives_` is
// not empty
BatchReceiveCallback popBatchReceiveCallback() {
auto callback = std::move(batchPendingReceives_.front().batchReceiveCallback_);
batchPendingReceives_.pop();
return callback;
}

friend class MultiTopicsConsumerImpl;
friend class PulsarFriend;
};
Expand Down
Loading