@cryptotaxi247 / netdata-1 / commits / 02fcff82d

Fix releasing statements after databases are closed (#20045)

Do not release statements if sqlite library has been shutdown

Stelios Fragkakis committed Apr 3, 2025 at 19:17 UTC 02fcff82d6c0707c0ebeb27f26e18c1f775b91f3
1 file changed +17 -4
src/database/sqlite/sqlite_functions.c
+17 -4
@@ -7,6 +7,10 @@ pthread_key_t key_pool[MAX_PREPARED_STATEMENTS];
7
8 long long def_journal_size_limit = 16777216;
9
10 +SPINLOCK sqlite_spinlock = SPINLOCK_INITIALIZER;
11 +
12 +bool sqlite_online;
13 +
14 SQLITE_API int sqlite3_exec_monitored(
15 sqlite3 *db, /* An open database */
16 const char *sql, /* SQL to be evaluated */
@@ -181,8 +185,12 @@ static void add_stmt_to_list(sqlite3_stmt *res)
185 static void release_statement(void *statement)
186 {
187 int rc;
184 - if (unlikely(rc = sqlite3_finalize((sqlite3_stmt *) statement) != SQLITE_OK))
185 - error_report("Failed to finalize statement, rc = %d", rc);
188 + spinlock_lock(&sqlite_spinlock);
189 + if (sqlite_online) {
190 + if (unlikely(rc = sqlite3_finalize((sqlite3_stmt *)statement) != SQLITE_OK))
191 + error_report("Failed to finalize statement, rc = %d", rc);
192 + }
193 + spinlock_unlock(&sqlite_spinlock);
194 }
195
196 static void initialize_thread_key_pool(void)
@@ -379,6 +387,10 @@ void sqlite_close_databases(void)
387 {
388 add_stmt_to_list(NULL);
389
390 + spinlock_lock(&sqlite_spinlock);
391 + sqlite_online = false;
392 + spinlock_unlock(&sqlite_spinlock);
393 +
394 sql_close_database(db_context_meta, "CONTEXT");
395 sql_close_database(db_meta, "METADATA");
396 }
@@ -404,6 +416,7 @@ uint64_t get_total_database_space(void)
416
417 int sqlite_library_init(void)
418 {
419 + spinlock_lock(&sqlite_spinlock);
420 initialize_thread_key_pool();
421
422 int rc = sqlite3_initialize();
@@ -424,12 +437,12 @@ int sqlite_library_init(void)
437 nd_log_daemon(
438 NDLP_INFO, "SQLITE: heap memory hard limit %s, soft limit %s", sqlite_hard_limit_mb, sqlite_soft_limit_mb);
439 }
440 + sqlite_online = true;
441 + spinlock_unlock(&sqlite_spinlock);
442
443 return (SQLITE_OK != rc);
444 }
445
431 -SPINLOCK sqlite_spinlock = SPINLOCK_INITIALIZER;
432 -
446 int sqlite_release_memory(int bytes)
447 {
448 return sqlite3_release_memory(bytes);