From 70434fc7001f9f50e11b03ace646d997d72b49c6 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 15 Jul 2024 21:34:58 -0700 Subject: [PATCH] [humble] Fix for failing throws_on_invalid_pragma_in_config_file test on Windows (backport #1742) (#1748) * Fix for failing throws_on_invalid_pragma_in_config_file on Windows (#1742) - The failure was because database file was not properly closed after throwing exception from the SqliteWrapper constructor and std::filesystem::remove_all(..) failed to delete temporary folder in the test fixture destructor. - Added reset for prepared sql statement before throwing exception. - Try to close database in constructor if we got exception after opening it since destructor will not be called in this case. Signed-off-by: Michael Orlov (cherry picked from commit 055935d33dd2fed2772657c9dc1f2173eaa7f752) # Conflicts: # rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_wrapper.cpp * Address merge conflicts Signed-off-by: Michael Orlov --------- Signed-off-by: Michael Orlov Co-authored-by: Michael Orlov --- .../sqlite/sqlite_statement_wrapper.cpp | 4 ++-- .../sqlite/sqlite_wrapper.cpp | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_statement_wrapper.cpp b/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_statement_wrapper.cpp index 9177b04ee6..495721ca15 100644 --- a/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_statement_wrapper.cpp +++ b/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_statement_wrapper.cpp @@ -60,7 +60,7 @@ std::shared_ptr SqliteStatementWrapper::execute_and_rese std::stringstream errmsg; errmsg << "Error when processing SQL statement. SQLite error (" << return_code << "): " << sqlite3_errstr(return_code); - + reset(); throw SqliteException{errmsg.str(), return_code}; } @@ -72,7 +72,7 @@ std::shared_ptr SqliteStatementWrapper::execute_and_rese std::stringstream errmsg; errmsg << "Statement returned empty value while result was expected: \'" << sqlite3_sql(statement_) << "\'"; - + reset(); throw SqliteException{errmsg.str(), return_code}; } } diff --git a/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_wrapper.cpp b/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_wrapper.cpp index 6c1435bfca..37541e17aa 100644 --- a/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_wrapper.cpp +++ b/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_wrapper.cpp @@ -61,8 +61,18 @@ SqliteWrapper::SqliteWrapper( } } - apply_pragma_settings(pragmas, io_flag); - sqlite3_extended_result_codes(db_ptr, 1); + try { + apply_pragma_settings(pragmas, io_flag); + sqlite3_extended_result_codes(db_ptr, 1); + } catch (...) { + const int rc = sqlite3_close(db_ptr); + if (rc != SQLITE_OK) { + ROSBAG2_STORAGE_DEFAULT_PLUGINS_LOG_ERROR_STREAM( + "Could not close open database. Error code: " << rc << + " Error message: " << sqlite3_errstr(rc)); + } + throw; + } } SqliteWrapper::SqliteWrapper()