@cryptotaxi247 / netdata-1 / commits / e9760b43f

Improve agent shutdown (#16959)

* Clear detection thread on stop Handle multi db close * cancel_threads should be called once

Stelios Fragkakis committed Feb 8, 2024 at 09:43 UTC e9760b43f53442d9dbd22f5353872ae5e83f7c4c
5 files changed +13 -2
src/daemon/main.c
+4
@@ -691,6 +691,9 @@ static void set_nofile_limit(struct rlimit *rl) {
691 void cancel_main_threads() {
692 nd_log_limits_unlimited();
693
694 + if (!static_threads)
695 + return;
696 +
697 int i, found = 0;
698 usec_t max = 5 * USEC_PER_SEC, step = 100000;
699 for (i = 0; static_threads[i].name != NULL ; i++) {
@@ -732,6 +735,7 @@ void cancel_main_threads() {
735 freez(static_threads[i].thread);
736
737 freez(static_threads);
738 + static_threads = NULL;
739 }
740
741 static const struct option_def {
src/database/sqlite/sqlite_context.c
+1
@@ -86,6 +86,7 @@ void sql_close_context_database(void)
86 rc = sqlite3_close_v2(db_context_meta);
87 if (unlikely(rc != SQLITE_OK))
88 error_report("Error %d while closing the context SQLite database, %s", rc, sqlite3_errstr(rc));
89 + db_context_meta = NULL;
90 }
91
92 //
src/database/sqlite/sqlite_functions.c
+1
@@ -518,6 +518,7 @@ void sql_close_database(void)
518 rc = sqlite3_close_v2(db_meta);
519 if (unlikely(rc != SQLITE_OK))
520 error_report("Error %d while closing the SQLite database, %s", rc, sqlite3_errstr(rc));
521 + db_meta = NULL;
522 }
523
524 int exec_statement_with_uuid(const char *sql, uuid_t *uuid)
src/database/sqlite/sqlite_metadata.c
+4 -1
@@ -1826,9 +1826,12 @@ void metadata_sync_shutdown(void)
1826
1827 void metadata_sync_shutdown_prepare(void)
1828 {
1829 - if (unlikely(!metasync_worker.loop))
1829 + static bool running = false;
1830 + if (unlikely(!metasync_worker.loop || running))
1831 return;
1832
1833 + running = true;
1834 +
1835 struct metadata_cmd cmd;
1836 memset(&cmd, 0, sizeof(cmd));
1837
src/ml/ml.cc
+3 -1
@@ -1805,12 +1805,13 @@ void ml_init()
1805 }
1806
1807 void ml_fini() {
1808 - if (!Cfg.enable_anomaly_detection)
1808 + if (!Cfg.enable_anomaly_detection || !db)
1809 return;
1810
1811 int rc = sqlite3_close_v2(db);
1812 if (unlikely(rc != SQLITE_OK))
1813 error_report("Error %d while closing the SQLite database, %s", rc, sqlite3_errstr(rc));
1814 + db = NULL;
1815 }
1816
1817 void ml_start_threads() {
@@ -1845,6 +1846,7 @@ void ml_stop_threads()
1846 return;
1847
1848 netdata_thread_join(Cfg.detection_thread, NULL);
1849 + Cfg.detection_thread = 0;
1850
1851 // signal the training queue of each thread
1852 for (size_t idx = 0; idx != Cfg.num_training_threads; idx++) {