@cryptotaxi247 / netdata-1 / commits / 13b34502c

Prevent core dump when the agent is performing a quick shutdown (#14587)

* Prevent core dump when the agent is performing a quick shutdown (e.g. when rrd_init fails) * Threads that have not started during shutdown are immediately marked as EXITED * Do not attempt to get statistics if database is not initialized * Do not attempt to get context db statistics if the context database is not initialized

Stelios Fragkakis committed Feb 24, 2023 at 11:41 UTC 13b34502c10d62c2210c1f624b37c96d3278b8a9
4 files changed +19 -3
daemon/main.c
+8 -3
@@ -656,9 +656,14 @@ void cancel_main_threads() {
656 int i, found = 0;
657 usec_t max = 5 * USEC_PER_SEC, step = 100000;
658 for (i = 0; static_threads[i].name != NULL ; i++) {
659 - if(static_threads[i].enabled == NETDATA_MAIN_THREAD_RUNNING) {
660 - info("EXIT: Stopping main thread: %s", static_threads[i].name);
661 - netdata_thread_cancel(*static_threads[i].thread);
659 + if (static_threads[i].enabled == NETDATA_MAIN_THREAD_RUNNING) {
660 + if (static_threads[i].thread) {
661 + info("EXIT: Stopping main thread: %s", static_threads[i].name);
662 + netdata_thread_cancel(*static_threads[i].thread);
663 + } else {
664 + info("EXIT: No thread running (marking as EXITED): %s", static_threads[i].name);
665 + static_threads[i].enabled = NETDATA_MAIN_THREAD_EXITED;
666 + }
667 found++;
668 }
669 }
database/sqlite/sqlite_context.c
+4
@@ -446,6 +446,10 @@ skip_delete:
446 int sql_context_cache_stats(int op)
447 {
448 int count, dummy;
449 +
450 + if (unlikely(!db_context_meta))
451 + return 0;
452 +
453 netdata_thread_disable_cancelability();
454 sqlite3_db_status(db_context_meta, op, &count, &dummy, 0);
455 netdata_thread_enable_cancelability();
database/sqlite/sqlite_functions.c
+4
@@ -955,6 +955,10 @@ int bind_text_null(sqlite3_stmt *res, int position, const char *text, bool can_b
955 int sql_metadata_cache_stats(int op)
956 {
957 int count, dummy;
958 +
959 + if (unlikely(!db_meta))
960 + return 0;
961 +
962 netdata_thread_disable_cancelability();
963 sqlite3_db_status(db_meta, op, &count, &dummy, 0);
964 netdata_thread_enable_cancelability();
database/sqlite/sqlite_metadata.c
+3
@@ -1272,6 +1272,9 @@ void metadata_sync_shutdown(void)
1272
1273 void metadata_sync_shutdown_prepare(void)
1274 {
1275 + if (unlikely(!metasync_worker.loop))
1276 + return;
1277 +
1278 struct metadata_cmd cmd;
1279 memset(&cmd, 0, sizeof(cmd));
1280