Skip to content
Closed
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
22 changes: 11 additions & 11 deletions lib/MultiTopicsConsumerImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -850,17 +850,17 @@ void MultiTopicsConsumerImpl::getBrokerConsumerStatsAsync(const BrokerConsumerSt
LatchPtr latchPtr = std::make_shared<Latch>(numberTopicPartitions_->load());
lock.unlock();

size_t i = 0;
consumers_.forEachValue([this, &latchPtr, &statsPtr, &i, callback](const ConsumerImplPtr& consumer) {
size_t index = i++;
auto weakSelf = weak_from_this();
consumer->getBrokerConsumerStatsAsync([this, weakSelf, latchPtr, statsPtr, index, callback](
Result result, const BrokerConsumerStats& stats) {
auto self = weakSelf.lock();
if (self) {
handleGetConsumerStats(result, stats, latchPtr, statsPtr, index, callback);
}
});
auto indexPtr = std::make_shared<std::atomic<size_t>>(0);
auto weakSelf = weak_from_this();
consumers_.forEachValue([weakSelf, latchPtr, statsPtr, indexPtr, callback](const ConsumerImplPtr& consumer) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The callback of forEachValue is immediately executed in the current method.

void forEachValue(ValueFunc&& each) {
Lock lock{mutex_};
for (auto&& kv : data_) {
each(kv.second);
}

so I believe this patch does not fix the actual issue

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but in consumers_.forEachValue, it also calls consumer->getBrokerConsumerStatsAsync, which passed the weakSelf into the callback. The callback is executed later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lambda of getBrokerConsumerStatsAsync captures all variables by value:

consumer->getBrokerConsumerStatsAsync([this, weakSelf, latchPtr, statsPtr, index, callback](

size_t index = indexPtr->fetch_add(1);
consumer->getBrokerConsumerStatsAsync([weakSelf, latchPtr, statsPtr, index, callback](
Result result, const BrokerConsumerStats& stats) {
auto self = weakSelf.lock();
if (self) {
self->handleGetConsumerStats(result, stats, latchPtr, statsPtr, index, callback);
}
});
});
}

Expand Down