]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1089] Protected last error concurrent accesses
authorFrancis Dupont <fdupont@isc.org>
Sat, 28 Mar 2020 09:17:11 +0000 (10:17 +0100)
committerFrancis Dupont <fdupont@isc.org>
Thu, 2 Apr 2020 09:27:57 +0000 (09:27 +0000)
src/lib/util/watched_thread.cc
src/lib/util/watched_thread.h

index ab9c30bb6e80917084eaf9f7ed9c165af1cd9cb2..df7a43917b1c603ca429902f47e8712ab5aec4f7 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
@@ -15,7 +15,10 @@ WatchedThread::start(const boost::function<void()>& thread_main) {
     clearReady(ERROR);
     clearReady(READY);
     clearReady(TERMINATE);
-    last_error_ = "no error";
+    {
+       std::lock_guard<std::mutex> lock(last_error_mutex_);
+       last_error_ = "no error";
+    }
     thread_.reset(new std::thread(thread_main));
 }
 
@@ -59,17 +62,22 @@ WatchedThread::stop() {
 
     clearReady(ERROR);
     clearReady(READY);
+    std::lock_guard<std::mutex> lock(last_error_mutex_);
     last_error_ = "thread stopped";
 }
 
 void
 WatchedThread::setError(const std::string& error_msg) {
-    last_error_ = error_msg;
+    {
+        std::lock_guard<std::mutex> lock(last_error_mutex_);
+        last_error_ = error_msg;
+    }
     markReady(ERROR);
 }
 
 std::string
 WatchedThread::getLastError() {
+    std::lock_guard<std::mutex> lock(last_error_mutex_);
     return (last_error_);
 }
 } // end of namespace isc::util
index b05294dc28f91cc4537b5fd6ed2d57e52fba3ebc..1cf9b4c34c9140bd9936a57c2f754966bde30970 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
@@ -12,6 +12,7 @@
 #include <boost/shared_ptr.hpp>
 #include <boost/function.hpp>
 
+#include <mutex>
 #include <thread>
 
 namespace isc {
@@ -109,6 +110,9 @@ public:
     /// @brief Error message of the last error encountered
     std::string last_error_;
 
+    /// @brief Mutex to protect last error message concurrent access
+    std::mutex last_error_mutex_;
+
     /// @brief WatchSockets that are used to communicate with the owning thread
     /// There are three:
     /// -# ERROR - Marked as ready by the thread when it experiences an error.