From f9a4bb18ffa1605cced3ee4b161675a0c88941be Mon Sep 17 00:00:00 2001 From: Tej Singh Date: Thu, 10 Sep 2020 23:20:58 -0700 Subject: [PATCH] Fix thread safety issue on clearing cache Historically, we havent held a lock on ForceClearCache and ClearCacheIfNecessary. This is not thread safe because kAllPullAtomInfo must be accessed in a lock, especially now that pullers can be registered/unregistered. Test: atest statsd_test, wrote a repro cl. Bug: 168156854 Change-Id: I47d53a6d9d274bca4c78dbfd87e0097091b7b8cb --- cmds/statsd/src/external/StatsPullerManager.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmds/statsd/src/external/StatsPullerManager.cpp b/cmds/statsd/src/external/StatsPullerManager.cpp index 46c377037542a..8334b6b4db908 100644 --- a/cmds/statsd/src/external/StatsPullerManager.cpp +++ b/cmds/statsd/src/external/StatsPullerManager.cpp @@ -315,6 +315,7 @@ void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) { } int StatsPullerManager::ForceClearPullerCache() { + std::lock_guard _l(mLock); int totalCleared = 0; for (const auto& pulledAtom : kAllPullAtomInfo) { totalCleared += pulledAtom.second->ForceClearCache(); @@ -323,6 +324,7 @@ int StatsPullerManager::ForceClearPullerCache() { } int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) { + std::lock_guard _l(mLock); int totalCleared = 0; for (const auto& pulledAtom : kAllPullAtomInfo) { totalCleared += pulledAtom.second->ClearCacheIfNecessary(timestampNs);