This is an automated email from the ASF dual-hosted git repository.

xyz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new 4e43a1d  [C++] Use weak ref to ClientConnection for timeout task 
(#12409)
4e43a1d is described below

commit 4e43a1dd85809f0242e354aef7a27973820e0dda
Author: Matteo Merli <[email protected]>
AuthorDate: Mon Oct 18 19:47:57 2021 -0700

    [C++] Use weak ref to ClientConnection for timeout task (#12409)
    
    ### Motivation
    
    Fixes #12408. Using a weak reference in the timeout task for 
`ClientConnection` to break a circular reference dependency between the 
connection instance and the task.
---
 pulsar-client-cpp/lib/ClientConnection.cc | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/pulsar-client-cpp/lib/ClientConnection.cc 
b/pulsar-client-cpp/lib/ClientConnection.cc
index 6f94731..4b8a3dc 100644
--- a/pulsar-client-cpp/lib/ClientConnection.cc
+++ b/pulsar-client-cpp/lib/ClientConnection.cc
@@ -532,18 +532,25 @@ void ClientConnection::handleResolve(const 
boost::system::error_code& err,
         return;
     }
 
-    auto self = shared_from_this();
-    connectTimeoutTask_->setCallback([this, self](const 
PeriodicTask::ErrorCode& ec) {
-        if (state_ != Ready) {
-            LOG_ERROR(cnxString_ << "Connection was not established in " << 
connectTimeoutTask_->getPeriodMs()
-                                 << " ms, close the socket");
+    auto self = ClientConnectionWeakPtr(shared_from_this());
+
+    connectTimeoutTask_->setCallback([self](const PeriodicTask::ErrorCode& ec) 
{
+        ClientConnectionPtr ptr = self.lock();
+        if (!ptr) {
+            // Connection was already destroyed
+            return;
+        }
+
+        if (ptr->state_ != Ready) {
+            LOG_ERROR(ptr->cnxString_ << "Connection was not established in "
+                                      << 
ptr->connectTimeoutTask_->getPeriodMs() << " ms, close the socket");
             PeriodicTask::ErrorCode err;
-            socket_->close(err);
+            ptr->socket_->close(err);
             if (err) {
-                LOG_WARN(cnxString_ << "Failed to close socket: " << 
err.message());
+                LOG_WARN(ptr->cnxString_ << "Failed to close socket: " << 
err.message());
             }
         }
-        connectTimeoutTask_->stop();
+        ptr->connectTimeoutTask_->stop();
     });
 
     LOG_DEBUG(cnxString_ << "Connecting to " << endpointIterator->endpoint() 
<< "...");

Reply via email to