From 5203c9d44110a194b67f1e7f5dccdb7733ba66f9 Mon Sep 17 00:00:00 2001 From: Wesley Shillingford Date: Fri, 3 Apr 2020 11:27:46 +0100 Subject: [PATCH] [TSAN] start_time data race in bootstrap_client --- nano/node/bootstrap/bootstrap_bulk_pull.cpp | 2 +- nano/node/bootstrap/bootstrap_connections.cpp | 11 +++++++++-- nano/node/bootstrap/bootstrap_connections.hpp | 6 +++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/nano/node/bootstrap/bootstrap_bulk_pull.cpp b/nano/node/bootstrap/bootstrap_bulk_pull.cpp index fc050471a3..99a6c77357 100644 --- a/nano/node/bootstrap/bootstrap_bulk_pull.cpp +++ b/nano/node/bootstrap/bootstrap_bulk_pull.cpp @@ -241,7 +241,7 @@ void nano::bulk_pull_client::received_block (boost::system::error_code const & e } if (connection->block_count++ == 0) { - connection->start_time = std::chrono::steady_clock::now (); + connection->set_start_time (std::chrono::steady_clock::now ()); } attempt->total_blocks++; bool stop_pull (attempt->process_block (block, known_account, pull_blocks, pull.count, block_expected, pull.retry_limit)); diff --git a/nano/node/bootstrap/bootstrap_connections.cpp b/nano/node/bootstrap/bootstrap_connections.cpp index 30cccef0a3..bf412f21b2 100644 --- a/nano/node/bootstrap/bootstrap_connections.cpp +++ b/nano/node/bootstrap/bootstrap_connections.cpp @@ -19,7 +19,7 @@ connections (connections_a), channel (channel_a), socket (socket_a), receive_buffer (std::make_shared> ()), -start_time (std::chrono::steady_clock::now ()) +start_time_m (std::chrono::steady_clock::now ()) { ++connections->connections_count; receive_buffer->resize (256); @@ -36,9 +36,16 @@ double nano::bootstrap_client::block_rate () const return static_cast (block_count.load () / elapsed); } +void nano::bootstrap_client::set_start_time (std::chrono::steady_clock::time_point start_time_a) +{ + nano::lock_guard guard (start_time_mutex); + start_time_m = start_time_a; +} + double nano::bootstrap_client::elapsed_seconds () const { - return std::chrono::duration_cast> (std::chrono::steady_clock::now () - start_time).count (); + nano::lock_guard guard (start_time_mutex); + return std::chrono::duration_cast> (std::chrono::steady_clock::now () - start_time_m).count (); } void nano::bootstrap_client::stop (bool force) diff --git a/nano/node/bootstrap/bootstrap_connections.hpp b/nano/node/bootstrap/bootstrap_connections.hpp index 652e20133f..85ef479a1f 100644 --- a/nano/node/bootstrap/bootstrap_connections.hpp +++ b/nano/node/bootstrap/bootstrap_connections.hpp @@ -26,15 +26,19 @@ class bootstrap_client final : public std::enable_shared_from_this node; std::shared_ptr connections; std::shared_ptr channel; std::shared_ptr socket; std::shared_ptr> receive_buffer; - std::chrono::steady_clock::time_point start_time; std::atomic block_count{ 0 }; std::atomic pending_stop{ false }; std::atomic hard_stop{ false }; + +private: + mutable std::mutex start_time_mutex; + std::chrono::steady_clock::time_point start_time_m; }; class bootstrap_connections final : public std::enable_shared_from_this