You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "dragonls (via GitHub)" <gi...@apache.org> on 2026/02/04 04:05:21 UTC

[PR] Fix use-after-free crash in `getBrokerConsumerStatsAsync` when connection is closed [pulsar-client-cpp]

dragonls opened a new pull request, #537:
URL: https://github.com/apache/pulsar-client-cpp/pull/537

   <!--
   ### Contribution Checklist
     
     - PR title format should be *[type][component] summary*. For details, see *[Guideline - Pulsar PR Naming Convention](https://docs.google.com/document/d/1d8Pw6ZbWk-_pCKdOmdvx9rnhPiyuxwq60_TrD68d7BA/edit#heading=h.trs9rsex3xom)*. 
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message
   
     - Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
   -->
   
   Fixes #536
   
   ### Motivation
   
   A use-after-free crash occurs in `MultiTopicsConsumerImpl::getBrokerConsumerStatsAsync()` when the underlying connection is closed while an asynchronous consumer stats request is pending.
   
   The root cause has two aspects:
   
   1. **Reference capture of stack variables**: The lambda captures local variables (`latchPtr`, `statsPtr`, `i`) by reference. If the callback is invoked after the function returns, these references become dangling.
   
   2. **Direct `this` capture in lambda**: Both the outer and inner lambdas capture `this` directly. Although there's a `weakSelf.lock()` check, the actual function call `handleGetConsumerStats(...)` is invoked through the captured `this` pointer (implicit `this->handleGetConsumerStats(...)`), **not through `self->`**. When `ClientConnection::close()` calls `setFailed()` on pending promises, it synchronously triggers the registered callbacks, potentially causing use-after-free if the `MultiTopicsConsumerImpl` has been destroyed.
   
   ### Modifications
   
   1. **Use `shared_ptr<atomic>` for index**: Replace stack variable `size_t i` with `std::shared_ptr<std::atomic<size_t>>` for thread-safe indexing and proper lifetime management.
   
   2. **Create `weakSelf` outside lambdas**: Move `weak_from_this()` call outside to avoid capturing `this` in the outer lambda.
   
   3. **Remove `this` capture from all lambdas**: Neither the outer nor inner lambda captures `this` anymore.
   
   4. **Call member function through `self->`**: Change `handleGetConsumerStats(...)` to `self->handleGetConsumerStats(...)` to ensure the call goes through the `shared_ptr`, not the raw `this` pointer.
   
   ### Verifying this change
   
   - [ ] Make sure that the change passes the CI checks.
   
   *(Please pick either of the following options)*
   
   This change is a trivial rework / code cleanup without any test coverage.
   
   The crash scenario is difficult to reproduce in unit tests as it requires precise timing of connection closure during async operations. However, the fix follows the standard C++ weak_ptr/shared_ptr pattern for preventing use-after-free in asynchronous callbacks.
   
   ### Documentation
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   - [ ] `doc-required` 
   (Your PR needs to update docs and you will update later)
   
   - [x] `doc-not-needed` 
   (Please explain why)
   
   - [ ] `doc` 
   (Your PR contains doc changes)
   
   - [ ] `doc-complete`
   (Docs have been already added)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix use-after-free crash in `getBrokerConsumerStatsAsync` when connection is closed [pulsar-client-cpp]

Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on code in PR #537:
URL: https://github.com/apache/pulsar-client-cpp/pull/537#discussion_r2762086912


##########
lib/MultiTopicsConsumerImpl.cc:
##########
@@ -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) {

Review Comment:
   The callback of `forEachValue` is immediately executed in the current method.
   
   https://github.com/apache/pulsar-client-cpp/blob/0b893a1622aa7dd702d48afee33af321bb2c06dd/lib/SynchronizedHashMap.h#L95-L99
   
   so I believe this patch does not fix the actual issue



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix use-after-free crash in `getBrokerConsumerStatsAsync` when connection is closed [pulsar-client-cpp]

Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on code in PR #537:
URL: https://github.com/apache/pulsar-client-cpp/pull/537#discussion_r2762416427


##########
lib/MultiTopicsConsumerImpl.cc:
##########
@@ -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) {

Review Comment:
   The lambda of `getBrokerConsumerStatsAsync` captures all variables by value:
   
   ```c++
   consumer->getBrokerConsumerStatsAsync([this, weakSelf, latchPtr, statsPtr, index, callback](
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix use-after-free crash in `getBrokerConsumerStatsAsync` when connection is closed [pulsar-client-cpp]

Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on PR #537:
URL: https://github.com/apache/pulsar-client-cpp/pull/537#issuecomment-3845567823

   I believe I've figured out the cause, let me create a new fix


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix use-after-free crash in `getBrokerConsumerStatsAsync` when connection is closed [pulsar-client-cpp]

Posted by "dragonls (via GitHub)" <gi...@apache.org>.
dragonls commented on PR #537:
URL: https://github.com/apache/pulsar-client-cpp/pull/537#issuecomment-3845589865

   > I believe I've figured out the cause, let me create a new fix
   
   Nice, looking forward to it.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix use-after-free crash in `getBrokerConsumerStatsAsync` when connection is closed [pulsar-client-cpp]

Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on PR #537:
URL: https://github.com/apache/pulsar-client-cpp/pull/537#issuecomment-3846205387

   Replace this PR by https://github.com/apache/pulsar-client-cpp/pull/538


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix use-after-free crash in `getBrokerConsumerStatsAsync` when connection is closed [pulsar-client-cpp]

Posted by "dragonls (via GitHub)" <gi...@apache.org>.
dragonls commented on code in PR #537:
URL: https://github.com/apache/pulsar-client-cpp/pull/537#discussion_r2762397005


##########
lib/MultiTopicsConsumerImpl.cc:
##########
@@ -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) {

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



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix use-after-free crash in `getBrokerConsumerStatsAsync` when connection is closed [pulsar-client-cpp]

Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower closed pull request #537: Fix use-after-free crash in `getBrokerConsumerStatsAsync` when connection is closed
URL: https://github.com/apache/pulsar-client-cpp/pull/537


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org