Skip to content

Commit 02fcff8

Browse files
authored
Fix releasing statements after databases are closed (netdata#20045)
Do not release statements if sqlite library has been shutdown
1 parent 62ea17c commit 02fcff8

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

src/database/sqlite/sqlite_functions.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ pthread_key_t key_pool[MAX_PREPARED_STATEMENTS];
77

88
long long def_journal_size_limit = 16777216;
99

10+
SPINLOCK sqlite_spinlock = SPINLOCK_INITIALIZER;
11+
12+
bool sqlite_online;
13+
1014
SQLITE_API int sqlite3_exec_monitored(
1115
sqlite3 *db, /* An open database */
1216
const char *sql, /* SQL to be evaluated */
@@ -181,8 +185,12 @@ static void add_stmt_to_list(sqlite3_stmt *res)
181185
static void release_statement(void *statement)
182186
{
183187
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);
186194
}
187195

188196
static void initialize_thread_key_pool(void)
@@ -379,6 +387,10 @@ void sqlite_close_databases(void)
379387
{
380388
add_stmt_to_list(NULL);
381389

390+
spinlock_lock(&sqlite_spinlock);
391+
sqlite_online = false;
392+
spinlock_unlock(&sqlite_spinlock);
393+
382394
sql_close_database(db_context_meta, "CONTEXT");
383395
sql_close_database(db_meta, "METADATA");
384396
}
@@ -404,6 +416,7 @@ uint64_t get_total_database_space(void)
404416

405417
int sqlite_library_init(void)
406418
{
419+
spinlock_lock(&sqlite_spinlock);
407420
initialize_thread_key_pool();
408421

409422
int rc = sqlite3_initialize();
@@ -424,12 +437,12 @@ int sqlite_library_init(void)
424437
nd_log_daemon(
425438
NDLP_INFO, "SQLITE: heap memory hard limit %s, soft limit %s", sqlite_hard_limit_mb, sqlite_soft_limit_mb);
426439
}
440+
sqlite_online = true;
441+
spinlock_unlock(&sqlite_spinlock);
427442

428443
return (SQLITE_OK != rc);
429444
}
430445

431-
SPINLOCK sqlite_spinlock = SPINLOCK_INITIALIZER;
432-
433446
int sqlite_release_memory(int bytes)
434447
{
435448
return sqlite3_release_memory(bytes);

0 commit comments

Comments
 (0)