Fix possible data race in `aos/network/multinode_timestamp_filter.h`
When debugging why the pylogcat tests time out sometimes, I ran the
tests with the thread sanitizer enabled. It complained about this data
race:
WARNING: ThreadSanitizer: data race (pid=23)
Write of size 8 at 0x7f01f677e810 by thread T3:
#0 aos::message_bridge::NewtonSolver::NewtonSolver() /proc/self/cwd/external/aos/aos/network/multinode_timestamp_filter.cc:88:62 (libexternal_Saos_Saos_Snetwork_Slibmultinode_Utimestamp_Ufilter.so+0x7facc) (BuildId: 125f6a2edddf59a1a30e77e67718c89d)
...
Previous write of size 8 at 0x7f01f677e810 by thread T4:
#0 aos::message_bridge::NewtonSolver::NewtonSolver() /proc/self/cwd/external/aos/aos/network/multinode_timestamp_filter.cc:88:62 (libexternal_Saos_Saos_Snetwork_Slibmultinode_Utimestamp_Ufilter.so+0x7facc) (BuildId: 125f6a2edddf59a1a30e77e67718c89d)
...
Location is global 'aos::message_bridge::NewtonSolver::solve_number_' of size 8 at 0x7f01f677e810 (libexternal_Saos_Saos_Snetwork_Slibmultinode_Utimestamp_Ufilter.so+0x154810)
This happens when you create multiple threads that each read from a
log. I suspect it's not a very common use case, but it does happen in
the pylogcat tests.
This patch makes the error go away by properly turning the variable
into an atomic.
Change-Id: Ie28e13a26a443cf1be165aff1cccad0582a64ef1
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/network/multinode_timestamp_filter.cc b/aos/network/multinode_timestamp_filter.cc
index 0a0385e..5476f01 100644
--- a/aos/network/multinode_timestamp_filter.cc
+++ b/aos/network/multinode_timestamp_filter.cc
@@ -82,7 +82,7 @@
}
} // namespace
-size_t NewtonSolver::solve_number_ = 0u;
+std::atomic<size_t> NewtonSolver::solve_number_ = 0u;
NewtonSolver::NewtonSolver() : my_solve_number_(solve_number_++) {}
diff --git a/aos/network/multinode_timestamp_filter.h b/aos/network/multinode_timestamp_filter.h
index 893f816..66dcbff 100644
--- a/aos/network/multinode_timestamp_filter.h
+++ b/aos/network/multinode_timestamp_filter.h
@@ -1,6 +1,7 @@
#ifndef AOS_NETWORK_MULTINODE_TIMESTAMP_FILTER_H_
#define AOS_NETWORK_MULTINODE_TIMESTAMP_FILTER_H_
+#include <atomic>
#include <functional>
#include <map>
#include <string_view>
@@ -171,7 +172,7 @@
size_t my_solve_number_;
// The global solve number counter used to deterministically find problems.
- static size_t solve_number_;
+ static std::atomic<size_t> solve_number_;
};
// A condensed representation of the time estimation problem statement. This is