From cb88d2ff048881e0d369dc6bdaf3d4e2a69296ff Mon Sep 17 00:00:00 2001 From: Doug Hoard Date: Fri, 27 Dec 2024 14:20:11 -0500 Subject: [PATCH] Fixed a potential concurrency issue when reloading configuration (#1108) Signed-off-by: dhoard --- .../src/main/java/io/prometheus/jmx/JmxCollector.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/collector/src/main/java/io/prometheus/jmx/JmxCollector.java b/collector/src/main/java/io/prometheus/jmx/JmxCollector.java index 6c0e5e98..304a531c 100644 --- a/collector/src/main/java/io/prometheus/jmx/JmxCollector.java +++ b/collector/src/main/java/io/prometheus/jmx/JmxCollector.java @@ -229,8 +229,9 @@ private void exitOnConfigError() { private void reloadConfig() { try (FileReader fr = new FileReader(configFile)) { Map newYamlConfig = new Yaml().load(fr); - config = loadConfig(newYamlConfig); - config.lastUpdate = configFile.lastModified(); + Config newConfig = loadConfig(newYamlConfig); + newConfig.lastUpdate = configFile.lastModified(); + config = newConfig; configReloadSuccess.inc(); } catch (Exception e) { LOGGER.log(SEVERE, "Configuration reload failed: %s: ", e); @@ -240,8 +241,8 @@ private void reloadConfig() { private synchronized Config getLatestConfig() { if (configFile != null) { - long mtime = configFile.lastModified(); - if (mtime > config.lastUpdate) { + long lastModified = configFile.lastModified(); + if (lastModified > config.lastUpdate) { LOGGER.log(FINE, "Configuration file changed, reloading..."); reloadConfig(); }