]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Make threads run until asked to stop.
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Fri, 8 Nov 2019 13:34:39 +0000 (14:34 +0100)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Fri, 8 Nov 2019 13:34:39 +0000 (14: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.

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

index 5e1d79432475867ec8fd0f19b5037a3e83ab2b05..bb2f617639b67220f55b1650f2eaef2e05e70687 100644 (file)
@@ -4313,7 +4313,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 +4450,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 +4519,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..a391190d87b087c6b269e9243dfdd4593dc04c8d 100644 (file)
@@ -53,6 +53,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()