Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[core] Recover from model cache write failures (#35132)

Nesterov Alexander240508667319
diff --git a/src/inference/src/cache_manager.hpp b/src/inference/src/cache_manager.hpp
index 199952486df..54f8d0e8d4b 100644
--- a/src/inference/src/cache_manager.hpp
+++ b/src/inference/src/cache_manager.hpp
@@ -12,7 +12,6 @@
#include <filesystem>
#include <fstream>
#include <functional>
-#include <memory>
#include <string>
#include <variant>
@@ -51,7 +50,16 @@ private:
// Fix the bug caused by pugixml, which may return unexpected results if the locale is different from "C".
ScopedLocale plocal_C(LC_ALL, "C");
const auto blob_path = get_blob_file(id);
- std::ofstream stream(blob_path, std::ios_base::binary);
+
+ if (ov::util::file_exists(blob_path)) {
+ std::filesystem::permissions(blob_path,
+ std::filesystem::perms::owner_write,
+ std::filesystem::perm_options::add);
+ }
+
+ std::ofstream stream;
+ stream.exceptions(std::ios_base::failbit | std::ios_base::badbit);
+ stream.open(blob_path, std::ios_base::binary);
writer(stream);
stream.close();
std::filesystem::permissions(blob_path,
@@ -62,7 +70,7 @@ private:
// Fix the bug caused by pugixml, which may return unexpected results if the locale is different from "C".
ScopedLocale plocal_C(LC_ALL, "C");
const auto blob_path = get_blob_file(id);
- if (std::filesystem::exists(blob_path)) {
+ if (ov::util::file_exists(blob_path)) {
if (enable_mmap) {
CompiledBlobVariant compiled_blob{std::in_place_index<0>, ov::read_tensor_data(blob_path)};
reader(compiled_blob);
@@ -76,8 +84,7 @@ private:
void remove_cache_entry(const std::string& id) override {
const auto blob_path = get_blob_file(id);
-
- if (std::filesystem::exists(blob_path)) {
+ if (ov::util::file_exists(blob_path)) {
std::ignore = std::filesystem::remove(blob_path);
}
}
diff --git a/src/inference/src/dev/core_impl.cpp b/src/inference/src/dev/core_impl.cpp
index 5c3af4886f4..a280a0c60e2 100644
--- a/src/inference/src/dev/core_impl.cpp
+++ b/src/inference/src/dev/core_impl.cpp
@@ -1546,6 +1546,10 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model_and_cache(ov::Plugin&
header_size_alignment);
compiled_model->export_model(stream);
});
+ } catch (const std::ios_base::failure&) {
+ cache_content.m_cache_manager->remove_cache_entry(cache_content.m_blob_id);
+ } catch (const ov::Exception&) {
+ cache_content.m_cache_manager->remove_cache_entry(cache_content.m_blob_id);
} catch (...) {
cache_content.m_cache_manager->remove_cache_entry(cache_content.m_blob_id);
throw;
diff --git a/src/inference/tests/functional/caching_test.cpp b/src/inference/tests/functional/caching_test.cpp
index 47747245ff9..4325abbc8ad 100644
--- a/src/inference/tests/functional/caching_test.cpp
+++ b/src/inference/tests/functional/caching_test.cpp
@@ -1638,7 +1638,7 @@ TEST_P(CachingTest, TestNoCachingProperties) {
}
}
-TEST_P(CachingTest, TestThrowOnExport) {
+TEST_P(CachingTest, TestThrowOnExportFailure) {
EXPECT_CALL(*mockPlugin, get_property(ov::supported_properties.name(), _)).Times(AnyNumber());
EXPECT_CALL(*mockPlugin, get_property(ov::device::capability::EXPORT_IMPORT, _)).Times(AnyNumber());
EXPECT_CALL(*mockPlugin, get_property(ov::device::architecture.name(), _)).Times(AnyNumber());
@@ -1657,6 +1657,66 @@ TEST_P(CachingTest, TestThrowOnExport) {
testLoad([&](ov::Core& core) {
core.set_property(ov::cache_dir(m_cacheDir));
EXPECT_ANY_THROW(m_testFunction(core));
+ EXPECT_FALSE(std::filesystem::exists(ov::util::make_path(m_cacheDir)) &&
+ std::filesystem::directory_iterator(ov::util::make_path(m_cacheDir)) !=
+ std::filesystem::directory_iterator{});
+ });
+ }
+}
+
+TEST_P(CachingTest, TestIgnoreOvExceptionExportFailure) {
+ EXPECT_CALL(*mockPlugin, get_property(ov::supported_properties.name(), _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::device::capability::EXPORT_IMPORT, _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::device::architecture.name(), _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::internal::supported_properties.name(), _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::internal::caching_properties.name(), _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::device::capabilities.name(), _)).Times(AnyNumber());
+ {
+ EXPECT_CALL(*mockPlugin, compile_model(_, _, _)).Times(m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin, compile_model(A<const std::shared_ptr<const ov::Model>&>(), _))
+ .Times(!m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _)).Times(0);
+ m_post_mock_net_callbacks.emplace_back([&](MockICompiledModelImpl& net) {
+ EXPECT_CALL(net, export_model(_)).Times(1).WillOnce(Invoke([&](std::ostream&) {
+ OPENVINO_THROW("export failed");
+ }));
+ });
+ testLoad([&](ov::Core& core) {
+ core.set_property(ov::cache_dir(m_cacheDir));
+ OV_ASSERT_NO_THROW(m_testFunction(core));
+ EXPECT_FALSE(std::filesystem::exists(ov::util::make_path(m_cacheDir)) &&
+ std::filesystem::directory_iterator(ov::util::make_path(m_cacheDir)) !=
+ std::filesystem::directory_iterator{});
+ });
+ }
+}
+
+TEST_P(CachingTest, TestIgnoreStreamExportFailure) {
+ EXPECT_CALL(*mockPlugin, get_property(ov::supported_properties.name(), _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::device::capability::EXPORT_IMPORT, _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::device::architecture.name(), _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::internal::supported_properties.name(), _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::internal::caching_properties.name(), _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::device::capabilities.name(), _)).Times(AnyNumber());
+ {
+ EXPECT_CALL(*mockPlugin, compile_model(_, _, _)).Times(m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin, compile_model(A<const std::shared_ptr<const ov::Model>&>(), _))
+ .Times(!m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _)).Times(0);
+ m_post_mock_net_callbacks.emplace_back([&](MockICompiledModelImpl& net) {
+ EXPECT_CALL(net, export_model(_)).Times(1).WillOnce(Invoke([&](std::ostream& stream) {
+ stream << "partial";
+ stream.setstate(std::ios_base::badbit);
+ }));
+ });
+ testLoad([&](ov::Core& core) {
+ core.set_property(ov::cache_dir(m_cacheDir));
+ OV_ASSERT_NO_THROW(m_testFunction(core));
+ EXPECT_FALSE(std::filesystem::exists(ov::util::make_path(m_cacheDir)) &&
+ std::filesystem::directory_iterator(ov::util::make_path(m_cacheDir)) !=
+ std::filesystem::directory_iterator{});
});
}
}
diff --git a/src/inference/tests/unit/cache_manager.cpp b/src/inference/tests/unit/cache_manager.cpp
new file mode 100644
index 00000000000..4d816ed9ba7
--- /dev/null
+++ b/src/inference/tests/unit/cache_manager.cpp
@@ -0,0 +1,124 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "cache_manager.hpp"
+
+#include <gtest/gtest.h>
+
+#include <fstream>
+#include <iterator>
+#include <memory>
+#include <stdexcept>
+#include <streambuf>
+#include <string>
+
+#include "common_test_utils/common_utils.hpp"
+
+namespace ov::test {
+namespace {
+
+std::string read_blob(const std::filesystem::path& path) {
+ std::ifstream stream(path, std::ios::binary);
+ return {std::istreambuf_iterator<char>(stream), std::istreambuf_iterator<char>()};
+}
+
+class FlushFailingStreambuf final : public std::streambuf {
+public:
+ explicit FlushFailingStreambuf(std::streambuf* delegate) : m_delegate(delegate) {}
+
+protected:
+ std::streamsize xsputn(const char* s, std::streamsize count) override {
+ return m_delegate->sputn(s, count);
+ }
+
+ int_type overflow(int_type ch) override {
+ if (traits_type::eq_int_type(ch, traits_type::eof())) {
+ return traits_type::not_eof(ch);
+ }
+ return m_delegate->sputc(traits_type::to_char_type(ch));
+ }
+
+ int sync() override {
+ return -1;
+ }
+
+private:
+ std::streambuf* m_delegate;
+};
+
+class FileStorageCacheManagerTest : public ::testing::Test {
+protected:
+ std::filesystem::path m_cache_dir;
+ std::unique_ptr<ICacheManager> m_cache_manager;
+
+ void SetUp() override {
+ m_cache_dir = ov::test::utils::generateTestFilePrefix();
+ m_cache_manager = std::make_unique<FileStorageCacheManager>(m_cache_dir);
+ }
+
+ void TearDown() override {
+ m_cache_manager.reset();
+ std::filesystem::remove_all(m_cache_dir);
+ }
+
+ std::filesystem::path blob_path(const std::string& id) const {
+ return m_cache_dir / (id + ".blob");
+ }
+};
+
+TEST_F(FileStorageCacheManagerTest, RemovesBlobAfterWriteFailureWhenCallerCleansUp) {
+ EXPECT_THROW(m_cache_manager->write_cache_entry("1",
+ [&](std::ostream& stream) {
+ stream << "partial";
+ throw std::runtime_error("write failed");
+ }),
+ std::runtime_error);
+
+ EXPECT_NO_THROW(m_cache_manager->remove_cache_entry("1"));
+ EXPECT_FALSE(std::filesystem::exists(blob_path("1")));
+}
+
+TEST_F(FileStorageCacheManagerTest, RemovesBlobAfterStreamFailureWhenCallerCleansUp) {
+ EXPECT_THROW(m_cache_manager->write_cache_entry("2",
+ [&](std::ostream& stream) {
+ stream << "partial";
+ stream.setstate(std::ios_base::badbit);
+ }),
+ std::ios_base::failure);
+
+ EXPECT_NO_THROW(m_cache_manager->remove_cache_entry("2"));
+ EXPECT_FALSE(std::filesystem::exists(blob_path("2")));
+}
+
+TEST_F(FileStorageCacheManagerTest, OverwritesExistingBlobWhenEntryAlreadyExists) {
+ m_cache_manager->write_cache_entry("7", [&](std::ostream& stream) {
+ stream << "cached";
+ });
+
+ bool writer_called = false;
+ EXPECT_NO_THROW(m_cache_manager->write_cache_entry("7", [&](std::ostream& stream) {
+ writer_called = true;
+ stream << "new";
+ }));
+
+ EXPECT_TRUE(writer_called);
+ EXPECT_EQ(read_blob(blob_path("7")), "new");
+}
+
+TEST_F(FileStorageCacheManagerTest, DoesNotLeaveAuxiliaryFilesInCacheDirectory) {
+ m_cache_manager->write_cache_entry("8", [&](std::ostream& stream) {
+ stream << "cached";
+ });
+
+ std::vector<std::filesystem::path> entries;
+ for (const auto& entry : std::filesystem::directory_iterator(m_cache_dir)) {
+ entries.push_back(entry.path().filename());
+ }
+
+ ASSERT_EQ(entries.size(), 1);
+ EXPECT_EQ(entries.front(), std::filesystem::path{"8.blob"});
+}
+
+} // namespace
+} // namespace ov::test