From 2762bbc71f5648a488b7dc29cb464183534d4e73 Mon Sep 17 00:00:00 2001 From: Matt-Esch Date: Mon, 9 May 2022 17:30:42 +0100 Subject: [PATCH] [fix][c++ client] avoid race condition causing double callback on close (#15508) * avoid race condition causing double callback on close * Update pulsar-client-cpp/lib/ClientImpl.cc Co-authored-by: Yunze Xu --- 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 6d9c4ed044ed4..dd00e3b9d4539 100644 --- a/pulsar-client-cpp/lib/ClientImpl.cc +++ b/pulsar-client-cpp/lib/ClientImpl.cc @@ -532,8 +532,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; + } 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