OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
Fix clang -Wthread-safety-analysis warning in cache_guard (#37025)
Tommy Chiangbfc3c0d09c3b
diff --git a/src/inference/src/cache_guard.cpp b/src/inference/src/cache_guard.cppindex 0e716058533..6e04231995d 100644--- a/src/inference/src/cache_guard.cpp+++ b/src/inference/src/cache_guard.cpp@@ -13,19 +13,21 @@ CacheGuardEntry::CacheGuardEntry(CacheGuard& cacheGuard,: m_cacheGuard(cacheGuard),m_hash(hash),m_mutex(std::move(m)),+ m_lock(*m_mutex, std::defer_lock),m_refCount(refCount) {- // Don't lock mutex right here for exception-safe considerationsm_refCount++;}CacheGuardEntry::~CacheGuardEntry() {m_refCount--;- m_mutex->unlock();+ if (m_lock.owns_lock()) {+ m_lock.unlock();+ }m_cacheGuard.check_for_remove(m_hash);}void CacheGuardEntry::perform_lock() {- m_mutex->lock();+ m_lock.lock();}//////////////////////////////////////////////////////diff --git a/src/inference/src/cache_guard.hpp b/src/inference/src/cache_guard.hppindex 26171966a50..2faca9f0944 100644--- a/src/inference/src/cache_guard.hpp+++ b/src/inference/src/cache_guard.hpp@@ -64,6 +64,8 @@ private:CacheGuard& m_cacheGuard;std::string m_hash;std::shared_ptr<std::mutex> m_mutex;+ // `m_lock' must be declared after `m_mutex' - see member initializer list+ std::unique_lock<std::mutex> m_lock;std::atomic_int& m_refCount;};diff --git a/src/inference/tests/unit/CMakeLists.txt b/src/inference/tests/unit/CMakeLists.txtindex bcfc65215b7..2f8a9a92c64 100644--- a/src/inference/tests/unit/CMakeLists.txt+++ b/src/inference/tests/unit/CMakeLists.txt@@ -11,6 +11,7 @@ endif()ov_add_test_target(NAME ${TARGET_NAME}SOURCES+ ${CMAKE_CURRENT_SOURCE_DIR}/cache_guard_test.cpp${CMAKE_CURRENT_SOURCE_DIR}/cache_manager.cpp${CMAKE_CURRENT_SOURCE_DIR}/compilation_context_test.cpp${CMAKE_CURRENT_SOURCE_DIR}/compiled_model_test.cppdiff --git a/src/inference/tests/unit/cache_guard_test.cpp b/src/inference/tests/unit/cache_guard_test.cppnew file mode 100644index 00000000000..1a03ddac50e--- /dev/null+++ b/src/inference/tests/unit/cache_guard_test.cpp@@ -0,0 +1,89 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "cache_guard.hpp"++#include <gtest/gtest.h>++#include <atomic>+#include <chrono>+#include <future>+#include <memory>+#include <mutex>+#include <thread>++namespace ov::test {++TEST(CacheGuardEntryTest, DestroyWithoutPerformLockDoesNotReleaseMutexHeldByAnotherThread) {+ CacheGuard guard;+ auto mutex = std::make_shared<std::mutex>();+ std::atomic_int refCount{0};++ // Another thread holds the per-hash mutex for the whole duration of the test.+ std::promise<void> locked;+ std::promise<void> release;+ std::thread holder([&] {+ std::lock_guard<std::mutex> lock(*mutex);+ locked.set_value();+ release.get_future().wait();+ });+ locked.get_future().wait();++ {+ // Entry is created but destroyed before perform_lock() is called.+ CacheGuardEntry entry(guard, "hash", mutex, refCount);+ EXPECT_EQ(refCount, 1);+ }+ EXPECT_EQ(refCount, 0);++ // The mutex is still owned by 'holder' - the entry must not have released it.+ EXPECT_FALSE(mutex->try_lock());++ release.set_value();+ holder.join();++ // Once the real owner is done, the mutex becomes available as usual.+ ASSERT_TRUE(mutex->try_lock());+ mutex->unlock();+}++TEST(CacheGuardEntryTest, DestroyAfterPerformLockReleasesMutex) {+ CacheGuard guard;+ auto mutex = std::make_shared<std::mutex>();+ std::atomic_int refCount{0};++ {+ CacheGuardEntry entry(guard, "hash", mutex, refCount);+ entry.perform_lock();+ EXPECT_EQ(refCount, 1);+ }+ EXPECT_EQ(refCount, 0);++ ASSERT_TRUE(mutex->try_lock());+ mutex->unlock();+}++TEST(CacheGuardTest, HashLockIsExclusivePerHashAndReleasedOnDestruction) {+ CacheGuard guard;+ auto lock = guard.get_hash_lock("hash");++ // A different hash is not blocked by the held lock.+ EXPECT_NO_THROW(guard.get_hash_lock("other_hash"));++ std::atomic<bool> acquired{false};+ std::thread waiter([&] {+ auto other = guard.get_hash_lock("hash");+ acquired = true;+ });++ // Best effort check that the waiter is blocked while 'lock' is alive.+ std::this_thread::sleep_for(std::chrono::milliseconds(100));+ EXPECT_FALSE(acquired);++ lock.reset();+ waiter.join();+ EXPECT_TRUE(acquired);+}++} // namespace ov::test