@cryptotaxi247 / netdata / commits / 4a95c7ab7

Close ML database after metasync workers finish (#22562)

Defer `ml_fini()` until after `metadata_sync_shutdown()` to prevent use-after-free on SQLite handle in metasync workers during shutdown.

Stelios Fragkakis committed May 25, 2026 at 23:10 UTC 4a95c7ab77c43f2103dbca612062c8966f2bdd66
1 file changed +6 -1
src/daemon/daemon-shutdown.c
+6 -1
@@ -239,7 +239,11 @@ static void netdata_cleanup_and_exit(EXIT_REASON reason, bool abnormal, bool exi
239 watcher_step_complete(WATCHER_STEP_ID_STOP_REPLICATION_THREADS);
240
241 ml_stop_threads();
242 - ml_fini();
242 + // ml_fini() (which closes ml_db) is deferred until after
243 + // metadata_sync_shutdown() drains the metasync workers below. Those
244 + // workers call ml_dimension_load_models() which uses ml_db; closing it
245 + // here exposes the metasync worker to a use-after-free on the SQLite
246 + // handle and triggers SIGSEGV inside sqlite3_prepare_v2 -> findElementWithHash.
247 watcher_step_complete(WATCHER_STEP_ID_DISABLE_ML_DETEC_AND_TRAIN_THREADS);
248
249 service_wait_exit(SERVICE_CONTEXT, 5 * USEC_PER_SEC);
@@ -307,6 +311,7 @@ static void netdata_cleanup_and_exit(EXIT_REASON reason, bool abnormal, bool exi
311 #endif
312
313 metadata_sync_shutdown();
314 + ml_fini();
315 watcher_step_complete(WATCHER_STEP_ID_STOP_METASYNC_THREADS);
316 }
317