@cryptotaxi247 / netdata-1 / commits / d6a762309

Close database if we encounter error during startup (#19600)

* Close database if there is an error to prevent further operations on it during shutdown * Better cleanup during initial open of the database

Stelios Fragkakis committed Feb 10, 2025 at 16:32 UTC d6a762309e398caa0d6a767b5049cacea7fd8059
1 file changed +9 -6
src/database/sqlite/sqlite_metadata.c
+9 -6
@@ -775,9 +775,7 @@ int sql_init_meta_database(db_check_action_type_t rebuild, int memory)
775 if (error_str)
776 analytics_set_data_str(&analytics_data.netdata_fail_reason, error_str);
777 freez(error_str);
778 - sqlite3_close(db_meta);
779 - db_meta = NULL;
780 - return 1;
778 + goto close_database;
779 }
780
781 if (rebuild & DB_CHECK_RECLAIM_SPACE) {
@@ -830,17 +828,22 @@ int sql_init_meta_database(db_check_action_type_t rebuild, int memory)
828 target_version = perform_database_migration(db_meta, DB_METADATA_VERSION);
829
830 if (configure_sqlite_database(db_meta, target_version, "meta_config"))
833 - return 1;
831 + goto close_database;
832
833 if (init_database_batch(db_meta, &database_config[0], "meta_init"))
836 - return 1;
834 + goto close_database;
835
836 if (init_database_batch(db_meta, &database_cleanup[0], "meta_cleanup"))
839 - return 1;
837 + goto close_database;
838
839 netdata_log_info("SQLite database initialization completed");
840
841 return 0;
842 +
843 +close_database:
844 + sqlite3_close(db_meta);
845 + db_meta = NULL;
846 + return 1;
847 }
848
849 // Metadata functions