@cryptotaxi247 / netdata-1 / commits / f4e1a05d6

Fix context unittest coredump (#14595)

* Fix compilation warning * Fix memory leak * Fix crash when calling -W ctxtest pthread keys not properly initialized when running only this test * Code cleanup

Stelios Fragkakis committed Feb 23, 2023 at 22:56 UTC f4e1a05d630a4c02b0ec3b2a511f09fa3d1f7921
4 files changed +18 -12
database/engine/datafile.c
+2 -2
@@ -379,8 +379,8 @@ static int scan_data_files_cmp(const void *a, const void *b)
379 /* Returns number of datafiles that were loaded or < 0 on error */
380 static int scan_data_files(struct rrdengine_instance *ctx)
381 {
382 - int ret, matched_files, failed_to_load;
383 - unsigned tier, no, i;
382 + int ret, matched_files, failed_to_load, i;
383 + unsigned tier, no;
384 uv_fs_t req;
385 uv_dirent_t dent;
386 struct rrdengine_datafile **datafiles, *datafile;
database/sqlite/sqlite_context.c
+8 -8
@@ -117,7 +117,6 @@ void sql_close_context_database(void)
117 rc = sqlite3_close_v2(db_context_meta);
118 if (unlikely(rc != SQLITE_OK))
119 error_report("Error %d while closing the context SQLite database, %s", rc, sqlite3_errstr(rc));
120 - return;
120 }
121
122 //
@@ -243,8 +242,6 @@ failed:
242 rc = sqlite3_reset(res);
243 if (rc != SQLITE_OK)
244 error_report("Failed to reset statement that fetches chart label data, rc = %d", rc);
246 -
247 - return;
245 }
246
247 // CONTEXT LIST
@@ -431,11 +428,11 @@ int ctx_delete_context(uuid_t *host_uuid, VERSIONED_CONTEXT_DATA *context_data)
428 if (rc_stored != SQLITE_DONE)
429 error_report("Failed to delete context %s, rc = %d", context_data->id, rc_stored);
430 #ifdef NETDATA_INTERNAL_CHECKS
434 - else {
435 - char host_uuid_str[UUID_STR_LEN];
436 - uuid_unparse_lower(*host_uuid, host_uuid_str);
437 - info("%s: Deleted context %s under host %s", __FUNCTION__ , context_data->id, host_uuid_str);
438 - }
431 + else {
432 + char host_uuid_str[UUID_STR_LEN];
433 + uuid_unparse_lower(*host_uuid, host_uuid_str);
434 + info("%s: Deleted context %s under host %s", __FUNCTION__, context_data->id, host_uuid_str);
435 + }
436 #endif
437
438 skip_delete:
@@ -489,6 +486,8 @@ int ctx_unittest(void)
486 uuid_t host_uuid;
487 uuid_generate(host_uuid);
488
489 + initialize_thread_key_pool();
490 +
491 int rc = sql_init_context_database(1);
492
493 if (rc != SQLITE_OK)
@@ -556,6 +555,7 @@ int ctx_unittest(void)
555 freez((void *)context_data.title);
556 freez((void *)context_data.chart_type);
557 freez((void *)context_data.family);
558 + freez((void *)context_data.units);
559
560 // The list should be empty
561 info("List context start after delete");
database/sqlite/sqlite_functions.c
+7 -2
@@ -156,6 +156,12 @@ static void release_statement(void *statement)
156 error_report("Failed to finalize statement, rc = %d", rc);
157 }
158
159 +void initialize_thread_key_pool(void)
160 +{
161 + for (int i = 0; i < MAX_PREPARED_STATEMENTS; i++)
162 + (void)pthread_key_create(&key_pool[i], release_statement);
163 +}
164 +
165 int prepare_statement(sqlite3 *database, const char *query, sqlite3_stmt **statement)
166 {
167 static __thread uint32_t keys_used = 0;
@@ -448,8 +454,7 @@ int sql_init_database(db_check_action_type_t rebuild, int memory)
454
455 info("SQLite database initialization completed");
456
451 - for (int i = 0; i < MAX_PREPARED_STATEMENTS; i++)
452 - (void)pthread_key_create(&key_pool[i], release_statement);
457 + initialize_thread_key_pool();
458
459 rc = sqlite3_create_function(db_meta, "u2h", 1, SQLITE_ANY | SQLITE_DETERMINISTIC, 0, sqlite_uuid_parse, 0, 0);
460 if (unlikely(rc != SQLITE_OK))
database/sqlite/sqlite_functions.h
+1
@@ -56,6 +56,7 @@ int prepare_statement(sqlite3 *database, const char *query, sqlite3_stmt **state
56 int execute_insert(sqlite3_stmt *res);
57 int exec_statement_with_uuid(const char *sql, uuid_t *uuid);
58 void db_execute(const char *cmd);
59 +void initialize_thread_key_pool(void);
60
61 // Look up functions
62 int get_node_id(uuid_t *host_id, uuid_t *node_id);