This is an automated email from the ASF dual-hosted git repository. penghui pushed a commit to branch branch-2.8 in repository https://gitbox.apache.org/repos/asf/pulsar.git
commit 44d85e8bbdf48713ac650039ee4ea838898f780a Author: Matteo Merli <[email protected]> AuthorDate: Tue Sep 7 18:37:36 2021 -0700 [C++] Handle error when shutting down client after forks (#11954) * [C++] Handle error when shutting down client after forks * Fixed formatting (cherry picked from commit 2858ed02a76788d1973a5145fca03997672c4779) --- pulsar-client-cpp/lib/ExecutorService.cc | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/pulsar-client-cpp/lib/ExecutorService.cc b/pulsar-client-cpp/lib/ExecutorService.cc index 7cd4a49..f7cb010 100644 --- a/pulsar-client-cpp/lib/ExecutorService.cc +++ b/pulsar-client-cpp/lib/ExecutorService.cc @@ -22,6 +22,9 @@ #include <functional> #include <memory> +#include "LogUtils.h" +DECLARE_LOG_OBJECT() + namespace pulsar { ExecutorService::ExecutorService() @@ -29,14 +32,7 @@ ExecutorService::ExecutorService() work_(new BackgroundWork(*io_service_)), worker_(std::bind(&ExecutorService::startWorker, this, io_service_)) {} -ExecutorService::~ExecutorService() { - close(); - // If the worker_ is still not joinable at this point just detach - // the thread so its destructor does not terminate the app - if (worker_.joinable()) { - worker_.detach(); - } -} +ExecutorService::~ExecutorService() { close(); } void ExecutorService::startWorker(std::shared_ptr<boost::asio::io_service> io_service) { io_service_->run(); } @@ -70,7 +66,13 @@ void ExecutorService::close() { work_.reset(); // Detach the worker thread instead of join to avoid potential deadlock if (worker_.joinable()) { - worker_.detach(); + try { + worker_.detach(); + } catch (const std::system_error &e) { + // This condition will happen if we're forking the process, therefore the thread was not ported to + // the child side of the fork and the detach would be failing. + LOG_DEBUG("Failed to detach thread: " << e.what()); + } } }
