From d3eaa2709f2f18288e85ec26b6171f1f1b1d5d6e Mon Sep 17 00:00:00 2001 From: Matt Esch Date: Mon, 9 May 2022 11:35:15 +0100 Subject: [PATCH 1/2] avoid race condition causing double callback on close --- pulsar-client-cpp/lib/ClientImpl.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pulsar-client-cpp/lib/ClientImpl.cc b/pulsar-client-cpp/lib/ClientImpl.cc index 60e3e248c127b..92438a9ea6581 100644 --- a/pulsar-client-cpp/lib/ClientImpl.cc +++ b/pulsar-client-cpp/lib/ClientImpl.cc @@ -536,8 +536,13 @@ void ClientImpl::handleClose(Result result, SharedInt numberOfOpenHandlers, Resu } if (*numberOfOpenHandlers == 0) { Lock lock(mutex_); - state_ = Closed; - lock.unlock(); + if (state_ == Closed) { + LOG_DEBUG("Client is already shutting down, possible race condition in handleClose"); + return lock.unlock(); + } else { + state_ = Closed; + lock.unlock(); + } LOG_DEBUG("Shutting down producers and consumers for client"); // handleClose() is called in ExecutorService's event loop, while shutdown() tried to wait the event From 871e0ac3405e98745e2a329dd3f807a1fcd584e9 Mon Sep 17 00:00:00 2001 From: Yunze Xu Date: Mon, 9 May 2022 20:04:24 +0800 Subject: [PATCH 2/2] Update pulsar-client-cpp/lib/ClientImpl.cc --- pulsar-client-cpp/lib/ClientImpl.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pulsar-client-cpp/lib/ClientImpl.cc b/pulsar-client-cpp/lib/ClientImpl.cc index 92438a9ea6581..931fadeaae789 100644 --- a/pulsar-client-cpp/lib/ClientImpl.cc +++ b/pulsar-client-cpp/lib/ClientImpl.cc @@ -538,7 +538,7 @@ void ClientImpl::handleClose(Result result, SharedInt numberOfOpenHandlers, Resu Lock lock(mutex_); if (state_ == Closed) { LOG_DEBUG("Client is already shutting down, possible race condition in handleClose"); - return lock.unlock(); + return; } else { state_ = Closed; lock.unlock();