Use destructor attribute for disposing thread 84/264984/3
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 6 Oct 2021 23:33:06 +0000 (08:33 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 6 Oct 2021 23:33:30 +0000 (08:33 +0900)
The rpc-port library can be loaded and unloaded using dlopen() and
dlclose() in the runtime. In this case, the process has crashed by
the thread of the rpc-port library. Before unloading the library, the thread
has to be terminated. This patch uses destructor attribute to terminate
the thread appropriately before unloading the library is finished.

Change-Id: I2338a4419f02fb67dff042fd308ba1d590bf0ac3
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/debug-port-internal.cc

index ddfd930..2f3219e 100644 (file)
 #include "debug-port-internal.hh"
 #include "log-private.hh"
 
+#undef RPC_DTOR
+#define RPC_DTOR __attribute__ ((destructor))
+
 namespace rpc_port {
 namespace internal {
 
 namespace {
 const char PATH_RPC_PORT_UTIL_SOCK[] = "/run/aul/daemons/.rpc-port-util-sock";
+
+RPC_DTOR void DebugPortDtor() {
+  DebugPort::GetInst()->Dispose();
+}
+
 }  // namespace
 
 std::atomic<DebugPort*> DebugPort::inst_;
 std::mutex DebugPort::mutex_;
 
 DebugPort::~DebugPort() {
-  if (!disposed_)
-    Dispose();
+  Dispose();
 }
 
 DebugPort* DebugPort::GetInst() {
@@ -59,6 +66,10 @@ DebugPort* DebugPort::GetInst() {
 }
 
 void DebugPort::Dispose() {
+  std::lock_guard<std::recursive_mutex> lock(GetMutex());
+  if (disposed_)
+    return;
+
   Unwatch();
   JoinThread();
   disposed_ = true;