You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by mm...@apache.org on 2021/09/08 01:38:19 UTC

[pulsar] branch master updated: [C++] Handle error when shutting down client after forks (#11954)

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

mmerli 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 2858ed0  [C++] Handle error when shutting down client after forks (#11954)
2858ed0 is described below

commit 2858ed02a76788d1973a5145fca03997672c4779
Author: Matteo Merli <mm...@apache.org>
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
---
 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 e0e382b..4db3112 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(); }
 
@@ -75,7 +71,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());
+        }
     }
 }