From 8b9fe48f13d49d414a94dfba748029c08657703d Mon Sep 17 00:00:00 2001 From: Tim Wojtulewicz Date: Wed, 19 Mar 2025 15:02:55 -0700 Subject: [PATCH] Fix data race with calling run_state::network_time from a separate thread --- src/storage/Manager.cc | 7 +++---- src/storage/Manager.h | 4 +++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/storage/Manager.cc b/src/storage/Manager.cc index b8b36198d6..1810b0d5f2 100644 --- a/src/storage/Manager.cc +++ b/src/storage/Manager.cc @@ -23,7 +23,7 @@ void detail::ExpirationTimer::Dispatch(double t, bool is_expire) { // in the interim. if ( ! expire_running.test_and_set() ) { DBG_LOG(DBG_STORAGE, "Starting new expiration thread"); - storage_mgr->expiration_thread = std::jthread([]() { storage_mgr->Expire(); }); + storage_mgr->expiration_thread = std::jthread([t]() { storage_mgr->Expire(t); }); } storage_mgr->StartExpirationTimer(); @@ -107,17 +107,16 @@ OperationResult Manager::CloseBackend(BackendPtr backend, ResultCallback* cb) { return res; } -void Manager::Expire() { +void Manager::Expire(double t) { // Expiration runs on a separate thread and loops over the vector of backends. The mutex // here ensures exclusive access. std::unique_lock lk(backends_mtx); DBG_LOG(DBG_STORAGE, "Expiration running, have %zu backends to check", backends.size()); - double current_network_time = run_state::network_time; for ( auto it = backends.begin(); it != backends.end() && ! run_state::terminating; ++it ) { if ( (*it)->IsOpen() ) - (*it)->Expire(current_network_time); + (*it)->Expire(t); } expire_running.clear(); diff --git a/src/storage/Manager.h b/src/storage/Manager.h index cb085d853e..ae3c1bab66 100644 --- a/src/storage/Manager.h +++ b/src/storage/Manager.h @@ -77,8 +77,10 @@ public: * Runs an expire operation on all open backends. This is called by the expiration * timer and shouldn't be called directly otherwise, since it should only happen on a * separate thread. + * + * @param t The network time that the expiration started. */ - void Expire(); + void Expire(double t); protected: friend class storage::detail::ExpirationTimer;