Skip to content

Commit

Permalink
Fixed a potential concurrency issue when reloading configuration (#1108)
Browse files Browse the repository at this point in the history
Signed-off-by: dhoard <doug.hoard@gmail.com>
  • Loading branch information
dhoard authored Dec 27, 2024
1 parent 9f1fdec commit cb88d2f
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions collector/src/main/java/io/prometheus/jmx/JmxCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,9 @@ private void exitOnConfigError() {
private void reloadConfig() {
try (FileReader fr = new FileReader(configFile)) {
Map<String, Object> 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);
Expand All @@ -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();
}
Expand Down

0 comments on commit cb88d2f

Please sign in to comment.