]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1095] Protected communication state timer concurrent accesses
authorFrancis Dupont <fdupont@isc.org>
Sat, 28 Mar 2020 13:36:10 +0000 (14:36 +0100)
committerRazvan Becheriu <razvan@isc.org>
Wed, 6 May 2020 09:46:31 +0000 (12:46 +0300)
src/hooks/dhcp/high_availability/communication_state.cc
src/hooks/dhcp/high_availability/communication_state.h

index f948976a29833f76a80accfa1798efc7f3b80d62..a0414c8d7840e834f3656428daaa652664815c47 100644 (file)
@@ -46,7 +46,8 @@ namespace ha {
 
 CommunicationState::CommunicationState(const IOServicePtr& io_service,
                                        const HAConfigPtr& config)
-    : io_service_(io_service), config_(config), timer_(), interval_(0),
+    : io_service_(io_service), config_(config), timer_(),
+      timer_mutex_(new std::mutex()), interval_(0),
       poke_time_(boost::posix_time::microsec_clock::universal_time()),
       heartbeat_impl_(0), partner_state_(-1), partner_scopes_(),
       clock_skew_(0, 0, 0, 0), last_clock_skew_warn_(),
@@ -93,6 +94,7 @@ CommunicationState::setPartnerScopes(ConstElementPtr new_scopes) {
 void
 CommunicationState::startHeartbeat(const long interval,
                                    const boost::function<void()>& heartbeat_impl) {
+    std::lock_guard<std::mutex> lock(*timer_mutex_);
     startHeartbeatInternal(interval, heartbeat_impl);
 }
 
@@ -139,6 +141,7 @@ CommunicationState::startHeartbeatInternal(const long interval,
 
 void
 CommunicationState::stopHeartbeat() {
+    std::lock_guard<std::mutex> lock(*timer_mutex_);
     if (timer_) {
         timer_->cancel();
         timer_.reset();
@@ -159,6 +162,7 @@ CommunicationState::poke() {
     // seems to be (re)established.
     clearUnackedClients();
 
+    std::lock_guard<std::mutex> lock(*timer_mutex_);
     if (timer_) {
         // Check the duration since last poke. If it is less than a second, we don't
         // want to reschedule the timer. The only case when the poke time duration is
index 1ced58daec15cb94ceeace696247928c16a5bd9c..9d0966dc54eababd2d0e29ca3ca656e06e14b4a9 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2018-2019 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2018-2020 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -17,6 +17,7 @@
 #include <boost/function.hpp>
 #include <boost/shared_ptr.hpp>
 #include <map>
+#include <mutex>
 #include <set>
 #include <string>
 
@@ -134,6 +135,7 @@ public:
     ///
     /// @return true if heartbeat is running, false otherwise.
     bool isHeartbeatRunning() const {
+        std::lock_guard<std::mutex> lock(*timer_mutex_);
         return (static_cast<bool>(timer_));
     }
 
@@ -306,6 +308,9 @@ protected:
     /// @brief Interval timer triggering heartbeat commands.
     asiolink::IntervalTimerPtr timer_;
 
+    /// @brief Mutex to protect timer_ concurrent accesses.
+    boost::shared_ptr<std::mutex> timer_mutex_;
+
     /// @brief Interval specified for the heartbeat.
     long interval_;