Skip to content

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

[core] File streams created by ov::pass::Serialize throws on error (#34724)

Pawel Raasz8ab19a9bbafe
diff --git a/src/core/src/pass/serialize.cpp b/src/core/src/pass/serialize.cpp
index 207b022af88..22c8b37d0b0 100644
--- a/src/core/src/pass/serialize.cpp
+++ b/src/core/src/pass/serialize.cpp
@@ -92,6 +92,16 @@ void serialize_func(std::ostream& xml_file,
ov::util::ConstantWriter constant_write_handler(bin_file);
serialize_func(xml_file, bin_file, std::move(model), ver, deterministic, constant_write_handler);
}
+
+void handle_file_serialize_error(const std::filesystem::path& xml_path,
+ const std::filesystem::path& bin_path,
+ std::ofstream& xml,
+ std::ofstream& bin) {
+ xml.close();
+ bin.close();
+ std::ignore = std::filesystem::remove(xml_path);
+ std::ignore = std::filesystem::remove(bin_path);
+}
} // namespace
namespace ov {
@@ -114,10 +124,12 @@ bool pass::Serialize::run_on_model(const std::shared_ptr<ov::Model>& model) {
std::ofstream bin_file(m_binPath, std::ios::binary);
OPENVINO_ASSERT(bin_file, "Can't open bin file: \"", m_binPath, "\"");
+ bin_file.exceptions(std::ofstream::failbit | std::ofstream::badbit);
// create xml file
std::ofstream xml_file(m_xmlPath);
OPENVINO_ASSERT(xml_file, "Can't open xml file: \"", m_xmlPath, "\"");
+ xml_file.exceptions(std::ofstream::failbit | std::ofstream::badbit);
try {
serialize_func(xml_file, bin_file, model, m_version);
@@ -125,10 +137,10 @@ bool pass::Serialize::run_on_model(const std::shared_ptr<ov::Model>& model) {
// optimization decision was made to create .bin file upfront and
// write to it directly instead of buffering its content in memory,
// hence we need to delete it here in case of failure
- xml_file.close();
- bin_file.close();
- std::ignore = std::filesystem::remove(m_xmlPath);
- std::ignore = std::filesystem::remove(m_binPath);
+ handle_file_serialize_error(m_xmlPath, m_binPath, xml_file, bin_file);
+ throw;
+ } catch (const std::ios_base::failure&) {
+ handle_file_serialize_error(m_xmlPath, m_binPath, xml_file, bin_file);
throw;
}
}
diff --git a/src/core/tests/pass/serialization/serialize.cpp b/src/core/tests/pass/serialization/serialize.cpp
index 2103f4447e6..756775d8e59 100644
--- a/src/core/tests/pass/serialization/serialize.cpp
+++ b/src/core/tests/pass/serialization/serialize.cpp
@@ -4,7 +4,7 @@
#include "openvino/pass/serialize.hpp"
-#include <gtest/gtest.h>
+#include <gmock/gmock.h>
#include <fstream>
#include <iterator>
@@ -93,7 +93,6 @@ TEST_P(SerializePassTestP, serialize_simple_model_with_constant) {
const auto& [is_valid, error_msg] = model_comparator().compare(serialized_model, m_model);
EXPECT_TRUE(is_valid) << error_msg;
}
-} // namespace ov::test
using SerializationParams = std::tuple<std::string, std::string>;
@@ -209,6 +208,35 @@ INSTANTIATE_TEST_SUITE_P(ONNXSerialization,
#endif
+TEST(SerializationManualTest, write_to_disk_full) {
+ // Create a small RAM disk (requires root or proper permissions)
+ // Alternative: use a small file-backed loopback device
+ const auto test_dir = std::filesystem::temp_directory_path() / "ov_small_disk";
+
+ if (!util::directory_exists(test_dir)) {
+ GTEST_SKIP()
+ << "Test requires a small disk at " << test_dir
+ << ". Please create it e.g. 'Mount small tmpfs: mount -t tmpfs -o size=1M tmpfs /tmp/ov_small_disk', "
+ "and rerun the test";
+ }
+
+ auto model = [] {
+ auto p1 = std::make_shared<Parameter>(element::u8, PartialShape{1});
+ auto c1 = std::make_shared<Constant>(element::u8, Shape{1, 1024, 1024}, std::vector<uint8_t>{10U});
+ auto add = std::make_shared<Add>(p1, c1);
+ return std::make_shared<ov::Model>(ov::OutputVector{add}, ov::ParameterVector{p1}, "simple_model");
+ }();
+
+ OV_EXPECT_THROW(ov::pass::Serialize(test_dir / "test_file.xml", {}).run_on_model(model),
+ std::ios_base::failure,
+ testing::_);
+
+ // clean-up on assertion fail, files will be created.
+ std::error_code ec;
+ std::filesystem::remove(test_dir / "test_file.xml", ec);
+ std::filesystem::remove(test_dir / "test_file.bin", ec);
+}
+
class MetaDataSerialize : public ov::test::TestsCommon {
public:
std::string ir_with_meta = R"V0G0N(
@@ -861,3 +889,4 @@ TEST_F(UndefinedTypeDynamicTypeSerializationTests, compare_dynamic_type_undefine
ASSERT_TRUE(files_equal(m_dynamic_type_out_xml_path, m_undefined_type_out_xml_path))
<< "Serialized XML files are different: dynamic type vs undefined type";
}
+} // namespace ov::test