@cryptotaxi247 / netdata-1 / commits / 3eeeb039a

Adjust lock to prevent crash during ML calculations (#21555)

Protect `dim->cns` with spinlocks to ensure thread-safe access if ml_host_stop is called

Stelios Fragkakis committed Jan 14, 2026 at 17:32 UTC 3eeeb039a8f9013730066a3fc11344839a779a56
1 file changed +6 -6
src/ml/ml.cc
+6 -6
@@ -747,9 +747,14 @@ ml_dimension_predict(ml_dimension_t *dim, calculated_number_t value, bool exists
747 if (dim->mls != MACHINE_LEARNING_STATUS_ENABLED)
748 return false;
749
750 + // Acquire lock to protect dim->cns from concurrent access by ml_host_stop()
751 + if (spinlock_trylock(&dim->slock) == 0)
752 + return false;
753 +
754 // Don't treat values that don't exist as anomalous
755 if (!exists) {
756 dim->cns.clear();
757 + spinlock_unlock(&dim->slock);
758 return false;
759 }
760
@@ -757,6 +762,7 @@ ml_dimension_predict(ml_dimension_t *dim, calculated_number_t value, bool exists
762 unsigned n = Cfg.diff_n + Cfg.max_samples_to_smooth + Cfg.lag_n;
763 if (dim->cns.size() < n) {
764 dim->cns.push_back(value);
765 + spinlock_unlock(&dim->slock);
766 return false;
767 }
768
@@ -785,12 +791,6 @@ ml_dimension_predict(ml_dimension_t *dim, calculated_number_t value, bool exists
791 };
792 ml_features_preprocess(&features, 1.0);
793
788 - /*
789 - * Lock to predict
790 - */
791 - if (spinlock_trylock(&dim->slock) == 0)
792 - return false;
793 -
794 // Mark the metric time as variable if we received different values
795 if (!same_value)
796 dim->mt = METRIC_TYPE_VARIABLE;