]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Fix race in registerAllStats() by making sure it only returns if the map
authorOtto <otto.moerbeek@open-xchange.com>
Wed, 27 Jan 2021 12:36:55 +0000 (13:36 +0100)
committerOtto <otto.moerbeek@open-xchange.com>
Wed, 27 Jan 2021 12:36:55 +0000 (13:36 +0100)
has been fully initialized. If another thread is busy initing, we wait a bit.
So it's safe to call getAllStatsMap() after registerAllStats() returns.
Fixes #10021

pdns/rec_channel_rec.cc
pdns/ws-recursor.cc

index dce7a12fe5a4b3923a5dae1e3c14f3d84fe24ef0..72192b2b909a23de9dd4d710e760c7a0d07d8b35 100644 (file)
@@ -1041,9 +1041,14 @@ extern ResponseStats g_rs;
 
 void registerAllStats()
 {
-  static std::atomic_flag s_init = ATOMIC_FLAG_INIT;
-  if(s_init.test_and_set())
+  static std::mutex s_lock;
+  static bool s_inited = false;
+
+  std::lock_guard<std::mutex> lock(s_lock);
+
+  if (s_inited) {
     return;
+  }
 
   addGetStat("questions", &g_stats.qcounter);
   addGetStat("ipv6-questions", &g_stats.ipv6qcounter);
@@ -1247,6 +1252,8 @@ void registerAllStats()
     const std::string name = "ecs-v6-response-bits-" + std::to_string(idx + 1);
     addGetStat(name, &(SyncRes::s_ecsResponsesBySubnetSize6.at(idx)));
   }
+  
+  s_inited = true;
 }
 
 void doExitGeneric(bool nicely)
index 6481c7cbd441fa7b5f9666c319f39c619a76f9d7..a6760bc3c85abc0bdead2bf2967985ab652116db 100644 (file)
@@ -984,7 +984,6 @@ RecursorWebServer::RecursorWebServer(FDMultiplexer* fdm)
   registerAllStats();
 
 #if CHECK_PROMETHEUS_METRICS
-  // There is a race here if another thread already called registerAllStats(); but it is not ready yet
   validatePrometheusMetrics();
 #endif