]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Make threads run until asked to stop.
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 11 Nov 2019 11:34:56 +0000 (12:34 +0100)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 11 Nov 2019 11:34:56 +0000 (12:34 +0100)
This is safer since the atexit handler is not ran while threads are
still active. Also, when using valgrind we get more clean leak reports.

Retry if the accidentally merged #8518 that was reverted.

pdns/pdns_recursor.cc
pdns/rec_channel.cc
pdns/rec_channel.hh
pdns/rec_channel_rec.cc

index d36625e4d9d6686cbbb96e3cc0a20d4e82117a7d..c6200a1e5f674d29c2abd1c2e24353f3ffc7f8c4 100644 (file)
@@ -4279,6 +4279,8 @@ static int serviceMain(int argc, char*argv[])
     infos.isListener = true;
     infos.isWorker = true;
     recursorThread(currentThreadId++, "worker");
+    
+    handlerInfos.thread.join();
   }
   else {
 
@@ -4313,7 +4315,9 @@ static int serviceMain(int argc, char*argv[])
     infos.isHandler = true;
     infos.thread = std::thread(recursorThread, 0, "web+stat");
 
-    s_threadInfos.at(0).thread.join();
+    for (auto & ti : s_threadInfos) {
+      ti.thread.join();
+    }
   }
   return 0;
 }
@@ -4448,7 +4452,8 @@ try
   time_t carbonInterval=::arg().asNum("carbon-interval");
   time_t luaMaintenanceInterval=::arg().asNum("lua-maintenance-interval");
   counter.store(0); // used to periodically execute certain tasks
-  for(;;) {
+
+  while (!RecursorControlChannel::stop) {
     while(MT->schedule(&g_now)); // MTasker letting the mthreads do their thing
 
     if(!(counter%500)) {
@@ -4516,6 +4521,7 @@ try
       }
     }
   }
+  return 0;
 }
 catch(PDNSException &ae) {
   g_log<<Logger::Error<<"Exception: "<<ae.reason<<endl;
index 0884afaa63cfeed350c879209f0d29fdd45ed6bd..65afbe092d8a61b49e61f64044213268baf1ec8c 100644 (file)
@@ -17,6 +17,8 @@
 
 #include "namespaces.hh"
 
+volatile sig_atomic_t RecursorControlChannel::stop;
+
 RecursorControlChannel::RecursorControlChannel()
 {
   d_fd=-1;
index ee653752ba610a4eae99d81d549e60264a5f029a..732bd81eacd0ae1514971a710e00364cd6eb5516 100644 (file)
@@ -27,6 +27,7 @@
 #include <vector>
 #include <inttypes.h>
 #include <sys/un.h>
+#include <signal.h>
 #include <pthread.h>
 #include "iputils.hh"
 #include "dnsname.hh"
@@ -53,6 +54,7 @@ public:
   std::string recv(std::string* remote=0, unsigned int timeout=5);
 
   int d_fd;
+  static volatile sig_atomic_t stop;
 private:
   struct sockaddr_un d_local;
 };
index a86500ba122f29e862b4681b04ed0fe25be23ed4..a294e62ac7a7d8dd820e0c4bd0596552f64b5eb6 100644 (file)
@@ -1165,15 +1165,16 @@ void doExitGeneric(bool nicely)
 {
   g_log<<Logger::Error<<"Exiting on user request"<<endl;
   extern RecursorControlChannel s_rcc;
-  s_rcc.~RecursorControlChannel(); 
+  s_rcc.~RecursorControlChannel();
 
   extern string s_pidfname;
-  if(!s_pidfname.empty()) 
+  if(!s_pidfname.empty())
     unlink(s_pidfname.c_str()); // we can at least try..
-  if(nicely)
-    exit(1);
-  else
+  if(nicely) {
+    RecursorControlChannel::stop = 1;
+  } else {
     _exit(1);
+  }
 }
 
 void doExit()