From 33b0799d6b702bcfc01fb086cfe6364c442817d3 Mon Sep 17 00:00:00 2001 From: "Antonio S. Musumeci" Date: Sun, 22 Nov 2026 01:25:44 -0500 Subject: [PATCH] fix: make the statfs cache timeout safe to change at runtime - Setting cache.statfs through the control file writes the timeout while request threads read it without a lock, a data race that can tear on 32-bit builds. Make it an atomic, as 4a665bbf did for the default minfreespace. - Load it once per call so the hit check and the refresh check use the same value. --- src/fs_statvfs_cache.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/fs_statvfs_cache.cpp b/src/fs_statvfs_cache.cpp index 245c5770..5fc6bbcc 100644 --- a/src/fs_statvfs_cache.cpp +++ b/src/fs_statvfs_cache.cpp @@ -21,6 +21,7 @@ #include "fs_statvfs.hpp" #include "statvfs_util.hpp" +#include #include #include #include @@ -40,8 +41,10 @@ typedef std::unordered_map statvfs_cache; // Cache is read heavy. Use a shared mutex so concurrent reads can // work in parallel and only inserts or refreshes take a unique lock. +// The timeout is changed at runtime (cache.statfs) while requests +// read it. static std::shared_mutex g_cache_mutex; -static u64 g_timeout = 0; +static std::atomic g_timeout{0}; static statvfs_cache g_cache; @@ -59,13 +62,13 @@ _get_time(void) u64 fs::statvfs_cache_timeout(void) { - return g_timeout; + return g_timeout.load(std::memory_order_relaxed); } void fs::statvfs_cache_timeout(cu64 timeout_) { - g_timeout = timeout_; + g_timeout.store(timeout_,std::memory_order_relaxed); } int @@ -73,8 +76,10 @@ fs::statvfs_cache(const std::string &path_, struct statvfs *st_) { u64 now; + u64 timeout; - if(g_timeout == 0) + timeout = g_timeout.load(std::memory_order_relaxed); + if(timeout == 0) return fs::statvfs(path_,st_); now = ::_get_time(); @@ -83,7 +88,7 @@ fs::statvfs_cache(const std::string &path_, std::shared_lock lk(g_cache_mutex); auto it = g_cache.find(path_); - if((it != g_cache.end()) && ((now - it->second.time) <= g_timeout)) + if((it != g_cache.end()) && ((now - it->second.time) <= timeout)) { *st_ = it->second.st; return 0; @@ -95,7 +100,7 @@ fs::statvfs_cache(const std::string &path_, // Only a successful statvfs refreshes the entry. A failure is // returned to the caller and retried on the next call. Element &e = g_cache[path_]; - if((now - e.time) > g_timeout) + if((now - e.time) > timeout) { int rv; struct statvfs st;